home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / texinfo / info / session.c < prev    next >
C/C++ Source or Header  |  1994-01-28  |  107KB  |  4,168 lines

  1. /* session.c -- The user windowing interface to Info. */
  2.  
  3. /* This file is part of GNU Info, a program for reading online documentation
  4.    stored in Info format.
  5.  
  6.    Copyright (C) 1993 Free Software Foundation, Inc.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2, or (at your option)
  11.    any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22.    Written by Brian Fox (bfox@ai.mit.edu). */
  23.  
  24. #include "info.h"
  25. #include <sys/file.h>
  26. #include <sys/ioctl.h>
  27. #include <fcntl.h>
  28.  
  29. #if defined (HAVE_SYS_TIME_H)
  30. #  include <sys/time.h>
  31. #  define HAVE_STRUCT_TIMEVAL
  32. #endif /* HAVE_SYS_TIME_H */
  33.  
  34. static void info_clear_pending_input (), info_set_pending_input ();
  35. static void info_handle_pointer ();
  36.  
  37. /* **************************************************************** */
  38. /*                                    */
  39. /*             Running an Info Session                */
  40. /*                                    */
  41. /* **************************************************************** */
  42.  
  43. /* The place that we are reading input from. */
  44. static FILE *info_input_stream = (FILE *)NULL;
  45.  
  46. /* The last executed command. */
  47. VFunction *info_last_executed_command = (VFunction *)NULL;
  48.  
  49. /* Becomes non-zero when 'q' is typed to an Info window. */
  50. int quit_info_immediately = 0;
  51.  
  52. /* Array of structures describing for each window which nodes have been
  53.    visited in that window. */
  54. INFO_WINDOW **info_windows = (INFO_WINDOW **)NULL;
  55.  
  56. /* Where to add the next window, if we need to add one. */
  57. static int info_windows_index = 0;
  58.  
  59. /* Number of slots allocated to INFO_WINDOWS. */
  60. static int info_windows_slots = 0;
  61.  
  62. void remember_window_and_node (), forget_window_and_nodes ();
  63. void initialize_info_session (), info_session ();
  64. void display_startup_message_and_start ();
  65.  
  66. /* Begin an info session finding the nodes specified by FILENAME and NODENAMES.
  67.    For each loaded node, create a new window.  Always split the largest of the
  68.    available windows. */
  69. void
  70. begin_multiple_window_info_session (filename, nodenames)
  71.      char *filename;
  72.      char **nodenames;
  73. {
  74.   register int i;
  75.   WINDOW *window = (WINDOW *)NULL;
  76.  
  77.   for (i = 0; nodenames[i]; i++)
  78.     {
  79.       NODE *node;
  80.  
  81.       node = info_get_node (filename, nodenames[i]);
  82.  
  83.       if (!node)
  84.     break;
  85.  
  86.       /* If this is the first node, initialize the info session. */
  87.       if (!window)
  88.     {
  89.       initialize_info_session (node);
  90.       window = active_window;
  91.     }
  92.       else
  93.     {
  94.       /* Find the largest window in WINDOWS, and make that be the active
  95.          one.  Then split it and add our window and node to the list
  96.          of remembered windows and nodes.  Then tile the windows. */
  97.       register WINDOW *win, *largest = (WINDOW *)NULL;
  98.       int max_height = 0;
  99.  
  100.       for (win = windows; win; win = win->next)
  101.         if (win->height > max_height)
  102.           {
  103.         max_height = win->height;
  104.         largest = win;
  105.           }
  106.  
  107.       if (!largest)
  108.         {
  109.           display_update_display (windows);
  110.           info_error (CANT_FIND_WIND);
  111.           info_session ();
  112.           exit (0);
  113.         }
  114.  
  115.       active_window = largest;
  116.       window = window_make_window (node);
  117.       if (window)
  118.         {
  119.           window_tile_windows (TILE_INTERNALS);
  120.           remember_window_and_node (window, node);
  121.         }
  122.       else
  123.         {
  124.           display_update_display (windows);
  125.           info_error (WIN_TOO_SMALL);
  126.           info_session ();
  127.           exit (0);
  128.         }
  129.     }
  130.     }
  131.   display_startup_message_and_start ();
  132. }
  133.  
  134. /* Start an info session with INITIAL_NODE, and an error message in the echo
  135.    area made from FORMAT and ARG. */
  136. void
  137. begin_info_session_with_error (initial_node, format, arg)
  138.      NODE *initial_node;
  139.      char *format;
  140.      void *arg;
  141. {
  142.   initialize_info_session (initial_node);
  143.   info_error (format, arg, (void *)NULL);
  144.   info_session ();
  145. }
  146.  
  147. /* Start an info session with INITIAL_NODE. */
  148. void
  149. begin_info_session (initial_node)
  150.      NODE *initial_node;
  151. {
  152.   initialize_info_session (initial_node);
  153.   display_startup_message_and_start ();
  154. }
  155.  
  156. void
  157. display_startup_message_and_start ()
  158. {
  159.   char *format;
  160.  
  161.   format = replace_in_documentation
  162.     ("Welcome to Info version %s.  Type \"\\[get-help-window]\" for help.");
  163.  
  164.   window_message_in_echo_area (format, version_string ());
  165.   info_session ();
  166. }
  167.  
  168. /* Run an info session with an already initialized window and node. */
  169. void
  170. info_session ()
  171. {
  172.   terminal_prep_terminal ();
  173.   display_update_display (windows);
  174.   info_last_executed_command = (VFunction *)NULL;
  175.   info_read_and_dispatch ();
  176.   terminal_unprep_terminal ();
  177.   close_dribble_file ();
  178. }
  179.  
  180. /* Here is a window-location dependent event loop.  Called from the
  181.    functions info_session (), and from read_xxx_in_echo_area (). */
  182. void
  183. info_read_and_dispatch ()
  184. {
  185.   unsigned char key;
  186.   int done;
  187.   done = 0;
  188.  
  189.   while (!done && !quit_info_immediately)
  190.     {
  191.       int lk;
  192.  
  193.       /* If we haven't just gone up or down a line, there is no
  194.      goal column for this window. */
  195.       if ((info_last_executed_command != info_next_line) &&
  196.       (info_last_executed_command != info_prev_line))
  197.     active_window->goal_column = -1;
  198.  
  199.       if (echo_area_is_active)
  200.     {
  201.       lk = echo_area_last_command_was_kill;
  202.       echo_area_prep_read ();
  203.     }
  204.  
  205.       if (!info_any_buffered_input_p ())
  206.     display_update_display (windows);
  207.  
  208.       display_cursor_at_point (active_window);
  209.       info_initialize_numeric_arg ();
  210.  
  211.       initialize_keyseq ();
  212.       key = info_get_input_char ();
  213.  
  214.       /* No errors yet.  We just read a character, that's all.  Only clear
  215.      the echo_area if it is not currently active. */
  216.       if (!echo_area_is_active)
  217.     window_clear_echo_area ();
  218.  
  219.       info_error_was_printed = 0;
  220.  
  221.       /* Do the selected command. */
  222.       info_dispatch_on_key (key, active_window->keymap);
  223.  
  224.       if (echo_area_is_active)
  225.     {
  226.       /* Echo area commands that do killing increment the value of
  227.          ECHO_AREA_LAST_COMMAND_WAS_KILL.  Thus, if there is no
  228.          change in the value of this variable, the last command
  229.          executed was not a kill command. */
  230.       if (lk == echo_area_last_command_was_kill)
  231.         echo_area_last_command_was_kill = 0;
  232.  
  233.       if (ea_last_executed_command == ea_newline ||
  234.           info_aborted_echo_area)
  235.         {
  236.           ea_last_executed_command = (VFunction *)NULL;
  237.           done = 1;
  238.         }
  239.  
  240.       if (info_last_executed_command == info_quit)
  241.         quit_info_immediately = 1;
  242.     }
  243.       else if (info_last_executed_command == info_quit)
  244.     done = 1;
  245.     }
  246. }
  247.  
  248. /* Found in signals.c */
  249. extern void initialize_info_signal_handler ();
  250.  
  251. /* Initialize the first info session by starting the terminal, window,
  252.    and display systems. */
  253. void
  254. initialize_info_session (node)
  255.      NODE *node;
  256. {
  257.   char *getenv (), *term_name;
  258.  
  259.   term_name = getenv ("TERM");
  260.   terminal_initialize_terminal (term_name);
  261.  
  262.   if (terminal_is_dumb_p)
  263.     {
  264.       if (!term_name)
  265.     term_name = "dumb";
  266.  
  267.       info_error (TERM_TOO_DUMB, term_name);
  268.       exit (1);
  269.     }
  270.   terminal_clear_screen ();
  271.   initialize_info_keymaps ();
  272.   window_initialize_windows (screenwidth, screenheight);
  273.   initialize_info_signal_handler ();
  274.   display_initialize_display (screenwidth, screenheight);
  275.   info_set_node_of_window (active_window, node);
  276.  
  277.   /* Tell the window system how to notify us when a window needs to be
  278.      asynchronously deleted (e.g., user resizes window very small). */
  279.   window_deletion_notifier = forget_window_and_nodes;
  280.  
  281.   /* If input has not been redirected yet, make it come from STDIN. */
  282.   if (!info_input_stream)
  283.     info_input_stream = stdin;
  284.  
  285.   info_windows_initialized_p = 1;
  286. }
  287.  
  288. /* Tell Info that input is coming from the file FILENAME. */
  289. void
  290. info_set_input_from_file (filename)
  291.      char *filename;
  292. {
  293.   FILE *stream;
  294.  
  295.   stream = fopen (filename, "r");
  296.  
  297.   if (!stream)
  298.     return;
  299.  
  300.   if (info_input_stream != stdin)
  301.     fclose (info_input_stream);
  302.  
  303.   info_input_stream = stream;
  304.  
  305.   if (stream != stdin)
  306.     display_inhibited = 1;
  307. }
  308.  
  309. /* Return the INFO_WINDOW containing WINDOW, or NULL if there isn't one. */
  310. static INFO_WINDOW *
  311. get_info_window_of_window (window)
  312.      WINDOW *window;
  313. {
  314.   register int i;
  315.   INFO_WINDOW *info_win = (INFO_WINDOW *)NULL;
  316.  
  317.   for (i = 0; info_windows && (info_win = info_windows[i]); i++)
  318.     if (info_win->window == window)
  319.       break;
  320.  
  321.   return (info_win);
  322. }
  323.  
  324. /* Reset the remembered pagetop and point of WINDOW to WINDOW's current
  325.    values if the window and node are the same as the current one being
  326.    displayed. */
  327. void
  328. set_remembered_pagetop_and_point (window)
  329.      WINDOW *window;
  330. {
  331.   INFO_WINDOW *info_win;
  332.  
  333.   info_win = get_info_window_of_window (window);
  334.  
  335.   if (!info_win)
  336.     return;
  337.  
  338.   if (info_win->nodes_index &&
  339.       (info_win->nodes[info_win->current] == window->node))
  340.     {
  341.       info_win->pagetops[info_win->current] = window->pagetop;
  342.       info_win->points[info_win->current] = window->point;
  343.     }
  344. }
  345.  
  346. void
  347. remember_window_and_node (window, node)
  348.      WINDOW *window;
  349.      NODE *node;
  350. {
  351.   INFO_WINDOW *info_win;
  352.  
  353.   /* See if we already have this window in our list. */
  354.   info_win = get_info_window_of_window (window);
  355.  
  356.   /* If the window wasn't already on our list, then make a new entry. */
  357.   if (!info_win)
  358.     {
  359.       info_win = (INFO_WINDOW *)xmalloc (sizeof (INFO_WINDOW));
  360.       info_win->window = window;
  361.       info_win->nodes = (NODE **)NULL;
  362.       info_win->pagetops = (int *)NULL;
  363.       info_win->points = (long *)NULL;
  364.       info_win->current = 0;
  365.       info_win->nodes_index = 0;
  366.       info_win->nodes_slots = 0;
  367.  
  368.       add_pointer_to_array (info_win, info_windows_index, info_windows,
  369.                 info_windows_slots, 10, INFO_WINDOW *);
  370.     }
  371.  
  372.   /* If this node, the current pagetop, and the current point are the
  373.      same as the last saved node and pagetop, don't really add this to
  374.      the list of history nodes. */
  375.   {
  376.     int ni = info_win->nodes_index - 1;
  377.  
  378.     if ((ni != -1) &&
  379.     (info_win->nodes[ni]->contents == node->contents) &&
  380.     (info_win->pagetops[ni] == window->pagetop) &&
  381.     (info_win->points[ni] == window->point))
  382.     return;
  383.   }
  384.  
  385.   /* Remember this node, the currently displayed pagetop, and the current
  386.      location of point in this window.  Because we are updating pagetops
  387.      and points as well as nodes, it is more efficient to avoid the
  388.      add_pointer_to_array macro here. */
  389.   if (info_win->nodes_index + 2 >= info_win->nodes_slots)
  390.     {
  391.       info_win->nodes = (NODE **)
  392.     xrealloc (info_win->nodes,
  393.           (info_win->nodes_slots += 20) * sizeof (NODE *));
  394.  
  395.       info_win->pagetops = (int *)
  396.     xrealloc (info_win->pagetops, info_win->nodes_slots * sizeof (int));
  397.  
  398.       info_win->points = (long *)
  399.     xrealloc (info_win->points, info_win->nodes_slots * sizeof (long));
  400.     }
  401.  
  402.   info_win->nodes[info_win->nodes_index] = node;
  403.   info_win->pagetops[info_win->nodes_index] = window->pagetop;
  404.   info_win->points[info_win->nodes_index] = window->point;
  405.   info_win->current = info_win->nodes_index++;
  406.   info_win->nodes[info_win->nodes_index] = (NODE *)NULL;
  407.   info_win->pagetops[info_win->nodes_index] = 0;
  408.   info_win->points[info_win->nodes_index] = 0;
  409. }
  410.  
  411. #define DEBUG_FORGET_WINDOW_AND_NODES
  412. #if defined (DEBUG_FORGET_WINDOW_AND_NODES)
  413. static void
  414. consistency_check_info_windows ()
  415. {
  416.   register int i;
  417.   INFO_WINDOW *info_win;
  418.  
  419.   for (i = 0; i < info_windows_index; i++)
  420.     {
  421.       WINDOW *win;
  422.  
  423.       for (win = windows; win; win = win->next)
  424.     if (win == info_windows[i]->window)
  425.       break;
  426.  
  427.       if (!win)
  428.     abort ();
  429.     }
  430. }
  431. #endif /* DEBUG_FORGET_WINDOW_AND_NODES */
  432.  
  433. /* Remove WINDOW and its associated list of nodes from INFO_WINDOWS. */
  434. void
  435. forget_window_and_nodes (window)
  436.      WINDOW *window;
  437. {
  438.   register int i;
  439.   INFO_WINDOW *info_win = (INFO_WINDOW *)NULL;
  440.  
  441.   for (i = 0; info_windows && (info_win = info_windows[i]); i++)
  442.     if (info_win->window == window)
  443.       break;
  444.  
  445.   /* If we found the window to forget, then do so. */
  446.   if (info_win)
  447.     {
  448.       while (i < info_windows_index)
  449.     info_windows[i] = info_windows[++i];
  450.  
  451.       info_windows_index--;
  452.       info_windows[info_windows_index] = (INFO_WINDOW *)NULL;
  453.  
  454.       if (info_win->nodes)
  455.     {
  456.       for (i = 0; info_win->nodes[i]; i++)
  457.         if (internal_info_node_p (info_win->nodes[i]))
  458.           free (info_win->nodes[i]);
  459.       free (info_win->nodes);
  460.  
  461.       maybe_free (info_win->pagetops);
  462.       maybe_free (info_win->points);
  463.     }
  464.  
  465.       free (info_win);
  466.     }
  467. #if defined (DEBUG_FORGET_WINDOW_AND_NODES)
  468.   consistency_check_info_windows ();
  469. #endif /* DEBUG_FORGET_WINDOW_AND_NODES */
  470. }
  471.  
  472. /* Set WINDOW to show NODE.  Remember the new window in our list of Info
  473.    windows.  If we are doing automatic footnote display, also try to display
  474.    the footnotes for this window. */
  475. void
  476. info_set_node_of_window (window, node)
  477.      WINDOW *window;
  478.      NODE *node;
  479. {
  480.   /* Put this node into the window. */
  481.   window_set_node_of_window (window, node);
  482.  
  483.   /* Remember this node and window in our list of info windows. */
  484.   remember_window_and_node (window, node);
  485.  
  486.   /* If doing auto-footnote display/undisplay, show the footnotes belonging
  487.      to this window's node. */
  488.   if (auto_footnotes_p)
  489.     info_get_or_remove_footnotes (window);
  490. }
  491.  
  492.  
  493. /* **************************************************************** */
  494. /*                                    */
  495. /*               Info Movement Commands                */
  496. /*                                    */
  497. /* **************************************************************** */
  498.  
  499. /* Change the pagetop of WINDOW to DESIRED_TOP, perhaps scrolling the screen
  500.    to do so. */
  501. void
  502. set_window_pagetop (window, desired_top)
  503.      WINDOW *window;
  504.      int desired_top;
  505. {
  506.   int point_line, old_pagetop;
  507.  
  508.   if (desired_top < 0)
  509.     desired_top = 0;
  510.   else if (desired_top > window->line_count)
  511.     desired_top = window->line_count - 1;
  512.  
  513.   if (window->pagetop == desired_top)
  514.     return;
  515.  
  516.   old_pagetop = window->pagetop;
  517.   window->pagetop = desired_top;
  518.  
  519.   /* Make sure that point appears in this window. */
  520.   point_line = window_line_of_point (window);
  521.   if ((point_line < window->pagetop) ||
  522.       ((point_line - window->pagetop) > window->height - 1))
  523.     window->point =
  524.       window->line_starts[window->pagetop] - window->node->contents;
  525.  
  526.   window->flags |= W_UpdateWindow;
  527.  
  528.   /* Find out which direction to scroll, and scroll the window in that
  529.      direction.  Do this only if there would be a savings in redisplay
  530.      time.  This is true if the amount to scroll is less than the height
  531.      of the window, and if the number of lines scrolled would be greater
  532.      than 10 % of the window's height. */
  533.   if (old_pagetop < desired_top)
  534.     {
  535.       int start, end, amount;
  536.  
  537.       amount = desired_top - old_pagetop;
  538.  
  539.       if ((amount >= window->height) ||
  540.       (((window->height - amount) * 10) < window->height))
  541.     return;
  542.  
  543.       start = amount + window->first_row;
  544.       end = window->height + window->first_row;
  545.  
  546.       display_scroll_display (start, end, -amount);
  547.     }
  548.   else
  549.     {
  550.       int start, end, amount;
  551.  
  552.       amount = old_pagetop - desired_top;
  553.  
  554.       if ((amount >= window->height) ||
  555.       (((window->height - amount) * 10) < window->height))
  556.     return;
  557.  
  558.       start = window->first_row;
  559.       end = (window->first_row + window->height) - amount;
  560.       display_scroll_display (start, end, amount);
  561.     }
  562. }
  563.  
  564. /* Immediately make WINDOW->point visible on the screen, and move the
  565.    terminal cursor there. */
  566. static void
  567. info_show_point (window)
  568.      WINDOW *window;
  569. {
  570.   int old_pagetop;
  571.  
  572.   old_pagetop = window->pagetop;
  573.   window_adjust_pagetop (window);
  574.   if (old_pagetop != window->pagetop)
  575.     {
  576.       int new_pagetop;
  577.  
  578.       new_pagetop = window->pagetop;
  579.       window->pagetop = old_pagetop;
  580.       set_window_pagetop (window, new_pagetop);
  581.     }
  582.  
  583.   if (window->flags & W_UpdateWindow)
  584.     display_update_one_window (window);
  585.  
  586.   display_cursor_at_point (window);
  587. }
  588.  
  589. /* Move WINDOW->point from OLD line index to NEW line index. */
  590. static void
  591. move_to_new_line (old, new, window)
  592.      int old, new;
  593.      WINDOW *window;
  594. {
  595.   if (old == -1)
  596.     {
  597.       info_error (CANT_FIND_POINT);
  598.     }
  599.   else
  600.     {
  601.       int goal;
  602.  
  603.       if (new >= window->line_count || new < 0)
  604.     return;
  605.  
  606.       goal = window_get_goal_column (window);
  607.       window->goal_column = goal;
  608.  
  609.       window->point = window->line_starts[new] - window->node->contents;
  610.       window->point += window_chars_to_goal (window->line_starts[new], goal);
  611.       info_show_point (window);
  612.     }
  613. }
  614.  
  615. /* Move WINDOW's point down to the next line if possible. */
  616. DECLARE_INFO_COMMAND (info_next_line, "Move down to the next line")
  617. {
  618.   int old_line, new_line;
  619.  
  620.   if (count < 0)
  621.     info_prev_line (window, -count, key);
  622.   else
  623.     {
  624.       old_line = window_line_of_point (window);
  625.       new_line = old_line + count;
  626.       move_to_new_line (old_line, new_line, window);
  627.     }
  628. }
  629.  
  630. /* Move WINDOW's point up to the previous line if possible. */
  631. DECLARE_INFO_COMMAND (info_prev_line, "Move up to the previous line")
  632. {
  633.   int old_line, new_line;
  634.  
  635.   if (count < 0)
  636.     info_next_line (window, -count, key);
  637.   else
  638.     {
  639.       old_line = window_line_of_point (window);
  640.       new_line = old_line - count;
  641.       move_to_new_line (old_line, new_line, window);
  642.     }
  643. }
  644.  
  645. /* Move WINDOW's point to the end of the true line. */
  646. DECLARE_INFO_COMMAND (info_end_of_line, "Move to the end of the line")
  647. {
  648.   register int point, len;
  649.   register char *buffer;
  650.  
  651.   buffer = window->node->contents;
  652.   len = window->node->nodelen;
  653.  
  654.   for (point = window->point;
  655.        (point < len) && (buffer[point] != '\n');
  656.        point++);
  657.  
  658.   if (point != window->point)
  659.     {
  660.       window->point = point;
  661.       info_show_point (window);
  662.     }
  663. }
  664.  
  665. /* Move WINDOW's point to the beginning of the true line. */
  666. DECLARE_INFO_COMMAND (info_beginning_of_line, "Move to the start of the line")
  667. {
  668.   register int point;
  669.   register char *buffer;
  670.  
  671.   buffer = window->node->contents;
  672.   point = window->point;
  673.  
  674.   for (; (point) && (buffer[point - 1] != '\n'); point--);
  675.  
  676.   /* If at a line start alreay, do nothing. */
  677.   if (point != window->point)
  678.     {
  679.       window->point = point;
  680.       info_show_point (window);
  681.     }
  682. }
  683.  
  684. /* Move point forward in the node. */
  685. DECLARE_INFO_COMMAND (info_forward_char, "Move forward a character")
  686. {
  687.   if (count < 0)
  688.     info_backward_char (window, -count, key);
  689.   else
  690.     {
  691.       window->point += count;
  692.  
  693.       if (window->point >= window->node->nodelen)
  694.     window->point = window->node->nodelen - 1;
  695.  
  696.       info_show_point (window);
  697.     }
  698. }
  699.  
  700. /* Move point backward in the node. */
  701. DECLARE_INFO_COMMAND (info_backward_char, "Move backward a character")
  702. {
  703.   if (count < 0)
  704.     info_forward_char (window, -count, key);
  705.   else
  706.     {
  707.       window->point -= count;
  708.  
  709.       if (window->point < 0)
  710.     window->point = 0;
  711.  
  712.       info_show_point (window);
  713.     }
  714. }
  715.  
  716. #define alphabetic(c) (islower (c) || isupper (c) || isdigit (c))
  717.  
  718. /* Move forward a word in this node. */
  719. DECLARE_INFO_COMMAND (info_forward_word, "Move forward a word")
  720. {
  721.   long point;
  722.   char *buffer;
  723.   int end, c;
  724.  
  725.   if (count < 0)
  726.     {
  727.       info_backward_word (window, -count, key);
  728.       return;
  729.     }
  730.  
  731.   point = window->point;
  732.   buffer = window->node->contents;
  733.   end = window->node->nodelen;
  734.  
  735.   while (count)
  736.     {
  737.       if (point + 1 >= end)
  738.     return;
  739.  
  740.       /* If we are not in a word, move forward until we are in one.
  741.      Then, move forward until we hit a non-alphabetic character. */
  742.       c = buffer[point];
  743.  
  744.       if (!alphabetic (c))
  745.     {
  746.       while (++point < end)
  747.         {
  748.           c = buffer[point];
  749.           if (alphabetic (c))
  750.         break;
  751.         }
  752.     }
  753.  
  754.       if (point >= end) return;
  755.  
  756.       while (++point < end)
  757.     {
  758.       c = buffer[point];
  759.       if (!alphabetic (c))
  760.         break;
  761.     }
  762.       --count;
  763.     }
  764.   window->point = point;
  765.   info_show_point (window);
  766. }
  767.  
  768. DECLARE_INFO_COMMAND (info_backward_word, "Move backward a word")
  769. {
  770.   long point;
  771.   char *buffer;
  772.   int c;
  773.  
  774.   if (count < 0)
  775.     {
  776.       info_forward_word (window, -count, key);
  777.       return;
  778.     }
  779.  
  780.   buffer = window->node->contents;
  781.   point = window->point;
  782.  
  783.   while (count)
  784.     {
  785.       if (point == 0)
  786.     break;
  787.  
  788.       /* Like info_forward_word (), except that we look at the
  789.      characters just before point. */
  790.  
  791.       c = buffer[point - 1];
  792.  
  793.       if (!alphabetic (c))
  794.     {
  795.       while (--point)
  796.         {
  797.           c = buffer[point - 1];
  798.           if (alphabetic (c))
  799.         break;
  800.         }
  801.     }
  802.  
  803.       while (point)
  804.     {
  805.       c = buffer[point - 1];
  806.       if (!alphabetic (c))
  807.         break;
  808.       else
  809.         --point;
  810.     }
  811.       --count;
  812.     }
  813.   window->point = point;
  814.   info_show_point (window);
  815. }
  816.  
  817. /* Here is a list of time counter names which correspond to ordinal numbers.
  818.    It is used to print "once" instead of "1". */
  819. static char *counter_names[] = {
  820.   "not at all", "once", "twice", "three", "four", "five", "six",
  821.   (char *)NULL
  822. };
  823.  
  824. /* Buffer used to return values from times_description (). */
  825. static char td_buffer[50];
  826.  
  827. /* Function returns a static string fully describing the number of times
  828.    present in COUNT. */
  829. static char *
  830. times_description (count)
  831.      int count;
  832. {
  833.   register int i;
  834.  
  835.   td_buffer[0] = '\0';
  836.  
  837.   for (i = 0; counter_names[i]; i++)
  838.     if (count == i)
  839.       break;
  840.  
  841.   if (counter_names[i])
  842.     sprintf (td_buffer, "%s%s", counter_names[i], count > 2 ? " times" : "");
  843.   else
  844.     sprintf (td_buffer, "%d times", count);
  845.  
  846.   return (td_buffer);
  847. }
  848.  
  849. /* Variable controlling the behaviour of default scrolling when you are
  850.    already at the bottom of a node.  Possible values are defined in session.h.
  851.    The meanings are:
  852.  
  853.    IS_Continuous    Try to get first menu item, or failing that, the
  854.             "Next:" pointer, or failing that, the "Up:" and
  855.             "Next:" of the up.
  856.    IS_NextOnly        Try to get "Next:" menu item.
  857.    IS_PageOnly        Simply give up at the bottom of a node. */
  858.  
  859. int info_scroll_behaviour = IS_Continuous;
  860.  
  861. /* Choices used by the completer when reading a value for the user-visible
  862.    variable "scroll-behaviour". */
  863. char *info_scroll_choices[] = {
  864.   "Continuous", "Next Only", "Page Only", (char *)NULL
  865. };
  866.  
  867. /* Move to 1st menu item, Next, Up/Next, or error in this window. */
  868. static void
  869. forward_move_node_structure (window, behaviour)
  870.      WINDOW *window;
  871.      int behaviour;
  872. {
  873.   switch (behaviour)
  874.     {
  875.     case IS_PageOnly:
  876.       info_error (AT_NODE_BOTTOM);
  877.       break;
  878.  
  879.     case IS_NextOnly:
  880.       info_next_label_of_node (window->node);
  881.       if (!info_parsed_nodename && !info_parsed_filename)
  882.     info_error ("No \"Next\" pointer for this node.");
  883.       else
  884.     {
  885.       window_message_in_echo_area ("Following \"Next\" node...");
  886.       info_handle_pointer ("Next", window);
  887.     }
  888.       break;
  889.  
  890.     case IS_Continuous:
  891.       {
  892.     /* First things first.  If this node contains a menu, move down
  893.        into the menu. */
  894.     {
  895.       REFERENCE **menu;
  896.  
  897.       menu = info_menu_of_node (window->node);
  898.  
  899.       if (menu)
  900.         {
  901.           info_free_references (menu);
  902.           window_message_in_echo_area ("Selecting first menu item...");
  903.           info_menu_digit (window, 1, '1');
  904.           return;
  905.         }
  906.     }
  907.  
  908.     /* Okay, this node does not contain a menu.  If it contains a
  909.        "Next:" pointer, use that. */
  910.     info_next_label_of_node (window->node);
  911.     if (info_label_was_found)
  912.       {
  913.         window_message_in_echo_area ("Selecting \"Next\" node...");
  914.         info_handle_pointer ("Next", window);
  915.         return;
  916.       }
  917.  
  918.     /* Okay, there wasn't a "Next:" for this node.  Move "Up:" until we
  919.        can move "Next:".  If that isn't possible, complain that there
  920.        are no more nodes. */
  921.     {
  922.       int up_counter, old_current;
  923.       INFO_WINDOW *info_win;
  924.  
  925.       /* Remember the current node and location. */
  926.       info_win = get_info_window_of_window (window);
  927.       old_current = info_win->current;
  928.  
  929.       /* Back up through the "Up:" pointers until we have found a "Next:"
  930.          that isn't the same as the first menu item found in that node. */
  931.       up_counter = 0;
  932.       while (!info_error_was_printed)
  933.         {
  934.           info_up_label_of_node (window->node);
  935.           if (info_label_was_found)
  936.         {
  937.           info_handle_pointer ("Up", window);
  938.           if (info_error_was_printed)
  939.             continue;
  940.  
  941.           up_counter++;
  942.  
  943.           info_next_label_of_node (window->node);
  944.  
  945.           /* If no "Next" pointer, keep backing up. */
  946.           if (!info_label_was_found)
  947.             continue;
  948.  
  949.           /* If this node's first menu item is the same as this node's
  950.              Next pointer, keep backing up. */
  951.           if (!info_parsed_filename)
  952.             {
  953.               REFERENCE **menu;
  954.               char *next_nodename;
  955.  
  956.               /* Remember the name of the Next node, since reading
  957.              the menu can overwrite the contents of the
  958.              info_parsed_xxx strings. */
  959.               next_nodename = savestring (info_parsed_nodename);
  960.  
  961.               menu = info_menu_of_node (window->node);
  962.               if (menu &&
  963.               (strcmp
  964.                (menu[0]->nodename, next_nodename) == 0))
  965.             {
  966.               info_free_references (menu);
  967.               free (next_nodename);
  968.               continue;
  969.             }
  970.               else
  971.             {
  972.               /* Restore the world to where it was before
  973.                  reading the menu contents. */
  974.               info_free_references (menu);
  975.               free (next_nodename);
  976.               info_next_label_of_node (window->node);
  977.             }
  978.             }
  979.  
  980.           /* This node has a "Next" pointer, and it is not the
  981.              same as the first menu item found in this node. */
  982.           window_message_in_echo_area
  983.             ("Moving \"Up\" %s, then \"Next\".",
  984.              times_description (up_counter));
  985.  
  986.           info_handle_pointer ("Next", window);
  987.           return;
  988.         }
  989.           else
  990.         {
  991.           /* No more "Up" pointers.  Print an error, and call it
  992.              quits. */
  993.           register int i;
  994.  
  995.           for (i = 0; i < up_counter; i++)
  996.             {
  997.               info_win->nodes_index--;
  998.               free (info_win->nodes[info_win->nodes_index]);
  999.               info_win->nodes[info_win->nodes_index] = (NODE *)NULL;
  1000.             }
  1001.           info_win->current = old_current;
  1002.           window->node = info_win->nodes[old_current];
  1003.           window->pagetop = info_win->pagetops[old_current];
  1004.           window->point = info_win->points[old_current];
  1005.           recalculate_line_starts (window);
  1006.           window->flags |= W_UpdateWindow;
  1007.           info_error ("No more nodes.");
  1008.         }
  1009.         }
  1010.     }
  1011.     break;
  1012.       }
  1013.     }
  1014. }
  1015.  
  1016. /* Move Prev, Up or error in WINDOW depending on BEHAVIOUR. */
  1017. static void
  1018. backward_move_node_structure (window, behaviour)
  1019.      WINDOW *window;
  1020.      int behaviour;
  1021. {
  1022.   switch (behaviour)
  1023.     {
  1024.     case IS_PageOnly:
  1025.       info_error (AT_NODE_TOP);
  1026.       break;
  1027.  
  1028.     case IS_NextOnly:
  1029.       info_prev_label_of_node (window->node);
  1030.       if (!info_parsed_nodename && !info_parsed_filename)
  1031.     info_error ("No \"Prev\" for this node.");
  1032.       else
  1033.     {
  1034.       window_message_in_echo_area ("Moving \"Prev\" in this window.");
  1035.       info_handle_pointer ("Prev", window);
  1036.     }
  1037.       break;
  1038.  
  1039.     case IS_Continuous:
  1040.       info_prev_label_of_node (window->node);
  1041.  
  1042.       if (!info_parsed_nodename && !info_parsed_filename)
  1043.     {
  1044.       info_up_label_of_node (window->node);
  1045.       if (!info_parsed_nodename && !info_parsed_filename)
  1046.         info_error ("No \"Prev\" or \"Up\" for this node.");
  1047.       else
  1048.         {
  1049.           window_message_in_echo_area ("Moving \"Up\" in this window.");
  1050.           info_handle_pointer ("Up", window);
  1051.         }
  1052.     }
  1053.       else
  1054.     {
  1055.       REFERENCE **menu;
  1056.       int inhibit_menu_traversing = 0;
  1057.  
  1058.       /* Watch out!  If this node's Prev is the same as the Up, then
  1059.          move Up.  Otherwise, we could move Prev, and then to the last
  1060.          menu item in the Prev.  This would cause the user to loop
  1061.          through a subsection of the info file. */
  1062.       if (!info_parsed_filename && info_parsed_nodename)
  1063.         {
  1064.           char *pnode;
  1065.  
  1066.           pnode = savestring (info_parsed_nodename);
  1067.           info_up_label_of_node (window->node);
  1068.  
  1069.           if (!info_parsed_filename && info_parsed_nodename &&
  1070.           strcmp (info_parsed_nodename, pnode) == 0)
  1071.         {
  1072.           /* The nodes are the same.  Inhibit moving to the last
  1073.              menu item. */
  1074.           free (pnode);
  1075.           inhibit_menu_traversing = 1;
  1076.         }
  1077.           else
  1078.         {
  1079.           free (pnode);
  1080.           info_prev_label_of_node (window->node);
  1081.         }
  1082.         }
  1083.  
  1084.       /* Move to the previous node.  If this node now contains a menu,
  1085.          and we have not inhibited movement to it, move to the node
  1086.          corresponding to the last menu item. */
  1087.       window_message_in_echo_area ("Moving \"Prev\" in this window.");
  1088.       info_handle_pointer ("Prev", window);
  1089.  
  1090.       if (!inhibit_menu_traversing)
  1091.         {
  1092.           while (!info_error_was_printed &&
  1093.              (menu = info_menu_of_node (window->node)))
  1094.         {
  1095.           info_free_references (menu);
  1096.           window_message_in_echo_area
  1097.             ("Moving to \"Prev\"'s last menu item.");
  1098.           info_menu_digit (window, 1, '0');
  1099.         }
  1100.         }
  1101.     }
  1102.       break;
  1103.     }
  1104. }
  1105.  
  1106. /* Move continuously forward through the node structure of this info file. */
  1107. DECLARE_INFO_COMMAND (info_global_next_node,
  1108.               "Move forwards or down through node structure")
  1109. {
  1110.   if (count < 0)
  1111.     info_global_prev_node (window, -count, key);
  1112.   else
  1113.     {
  1114.       while (count && !info_error_was_printed)
  1115.     {
  1116.       forward_move_node_structure (window, IS_Continuous);
  1117.       count--;
  1118.     }
  1119.     }
  1120. }
  1121.  
  1122. /* Move continuously backward through the node structure of this info file. */
  1123. DECLARE_INFO_COMMAND (info_global_prev_node,
  1124.               "Move backwards or up through node structure")
  1125. {
  1126.   if (count < 0)
  1127.     info_global_next_node (window, -count, key);
  1128.   else
  1129.     {
  1130.       while (count && !info_error_was_printed)
  1131.     {
  1132.       backward_move_node_structure (window, IS_Continuous);
  1133.       count--;
  1134.     }
  1135.     }
  1136. }
  1137.  
  1138. /* Show the next screen of WINDOW's node. */
  1139. DECLARE_INFO_COMMAND (info_scroll_forward, "Scroll forward in this window")
  1140. {
  1141.   if (count < 0)
  1142.     info_scroll_backward (window, -count, key);
  1143.   else
  1144.     {
  1145.       int desired_top;
  1146.  
  1147.       /* Without an explicit numeric argument, scroll the bottom two
  1148.      lines to the top of this window,  Or, if at bottom of window,
  1149.      and the user wishes to scroll through nodes get the "Next" node
  1150.      for this window. */
  1151.       if (!info_explicit_arg && count == 1)
  1152.     {
  1153.       desired_top = window->pagetop + (window->height - 2);
  1154.  
  1155.       /* If there are no more lines to scroll here, error, or get
  1156.          another node, depending on INFO_SCROLL_BEHAVIOUR. */
  1157.       if (desired_top > window->line_count)
  1158.         {
  1159.           int behaviour = info_scroll_behaviour;
  1160.  
  1161.           /* Here is a hack.  If the key being used is not SPC, do the
  1162.          PageOnly behaviour. */
  1163.           if (key != SPC && key != DEL)
  1164.         behaviour = IS_PageOnly;
  1165.  
  1166.           forward_move_node_structure (window, behaviour);
  1167.           return;
  1168.         }
  1169.     }
  1170.       else
  1171.     desired_top = window->pagetop + count;
  1172.  
  1173.       if (desired_top >= window->line_count)
  1174.     desired_top = window->line_count - 2;
  1175.  
  1176.       if (window->pagetop > desired_top)
  1177.     return;
  1178.       else
  1179.     set_window_pagetop (window, desired_top);
  1180.     }
  1181. }
  1182.  
  1183. /* Show the previous screen of WINDOW's node. */
  1184. DECLARE_INFO_COMMAND (info_scroll_backward, "Scroll backward in this window")
  1185. {
  1186.   if (count < 0)
  1187.     info_scroll_forward (window, -count, key);
  1188.   else
  1189.     {
  1190.       int desired_top;
  1191.  
  1192.       /* Without an explicit numeric argument, scroll the top two lines
  1193.      to the bottom of this window, or move to the previous, or Up'th
  1194.      node. */
  1195.       if (!info_explicit_arg && count == 1)
  1196.     {
  1197.       desired_top = window->pagetop - (window->height - 2);
  1198.  
  1199.       if ((desired_top < 0) && (window->pagetop == 0))
  1200.         {
  1201.           int behaviour = info_scroll_behaviour;
  1202.  
  1203.           /* Same kind of hack as in info_scroll_forward.  If the key
  1204.          used to invoke this command is not DEL, do only the PageOnly
  1205.          behaviour. */
  1206.           if (key != DEL && key != SPC)
  1207.         behaviour = IS_PageOnly;
  1208.  
  1209.           backward_move_node_structure (window, behaviour);
  1210.           return;
  1211.         }
  1212.     }
  1213.       else
  1214.     desired_top = window->pagetop - count;
  1215.  
  1216.       if (desired_top < 0)
  1217.     desired_top = 0;
  1218.  
  1219.       set_window_pagetop (window, desired_top);
  1220.     }
  1221. }
  1222.  
  1223. /* Move to the beginning of the node. */
  1224. DECLARE_INFO_COMMAND (info_beginning_of_node, "Move to the start of this node")
  1225. {
  1226.   window->pagetop = window->point = 0;
  1227.   window->flags |= W_UpdateWindow;
  1228. }
  1229.  
  1230. /* Move to the end of the node. */
  1231. DECLARE_INFO_COMMAND (info_end_of_node, "Move to the end of this node")
  1232. {
  1233.   window->point = window->node->nodelen - 1;
  1234.   info_show_point (window);
  1235. }
  1236.  
  1237. /* **************************************************************** */
  1238. /*                                    */
  1239. /*           Commands for Manipulating Windows            */
  1240. /*                                    */
  1241. /* **************************************************************** */
  1242.  
  1243. /* Make the next window in the chain be the active window. */
  1244. DECLARE_INFO_COMMAND (info_next_window, "Select the next window")
  1245. {
  1246.   if (count < 0)
  1247.     {
  1248.       info_prev_window (window, -count, key);
  1249.       return;
  1250.     }
  1251.  
  1252.   /* If no other window, error now. */
  1253.   if (!windows->next && !echo_area_is_active)
  1254.     {
  1255.       info_error (ONE_WINDOW);
  1256.       return;
  1257.     }
  1258.  
  1259.   while (count--)
  1260.     {
  1261.       if (window->next)
  1262.     window = window->next;
  1263.       else
  1264.     {
  1265.       if (window == the_echo_area || !echo_area_is_active)
  1266.         window = windows;
  1267.       else
  1268.         window = the_echo_area;
  1269.     }
  1270.     }
  1271.  
  1272.   if (active_window != window)
  1273.     {
  1274.       if (auto_footnotes_p)
  1275.     info_get_or_remove_footnotes (window);
  1276.  
  1277.       window->flags |= W_UpdateWindow;
  1278.       active_window = window;
  1279.     }
  1280. }
  1281.  
  1282. /* Make the previous window in the chain be the active window. */
  1283. DECLARE_INFO_COMMAND (info_prev_window, "Select the previous window")
  1284. {
  1285.   if (count < 0)
  1286.     {
  1287.       info_next_window (window, -count, key);
  1288.       return;
  1289.     }
  1290.  
  1291.   /* Only one window? */
  1292.  
  1293.   if (!windows->next && !echo_area_is_active)
  1294.     {
  1295.       info_error (ONE_WINDOW);
  1296.       return;
  1297.     }
  1298.  
  1299.   while (count--)
  1300.     {
  1301.       /* If we are in the echo area, or if the echo area isn't active and we
  1302.      are in the first window, find the last window in the chain. */
  1303.       if (window == the_echo_area ||
  1304.       (window == windows && !echo_area_is_active))
  1305.     {
  1306.       register WINDOW *win, *last;
  1307.  
  1308.       for (win = windows; win; win = win->next)
  1309.         last = win;
  1310.  
  1311.       window = last;
  1312.     }
  1313.       else
  1314.     {
  1315.       if (window == windows)
  1316.         window = the_echo_area;
  1317.       else
  1318.         window = window->prev;
  1319.     }
  1320.     }
  1321.  
  1322.   if (active_window != window)
  1323.     {
  1324.       if (auto_footnotes_p)
  1325.     info_get_or_remove_footnotes (window);
  1326.  
  1327.       window->flags |= W_UpdateWindow;
  1328.       active_window = window;
  1329.     }
  1330. }
  1331.  
  1332. /* Split WINDOW into two windows, both showing the same node.  If we
  1333.    are automatically tiling windows, re-tile after the split. */
  1334. DECLARE_INFO_COMMAND (info_split_window, "Split the current window")
  1335. {
  1336.   WINDOW *split, *old_active;
  1337.   int pagetop;
  1338.  
  1339.   /* Remember the current pagetop of the window being split.  If it doesn't
  1340.      change, we can scroll its contents around after the split. */
  1341.   pagetop = window->pagetop;
  1342.  
  1343.   /* Make the new window. */
  1344.   old_active = active_window;
  1345.   active_window = window;
  1346.   split = window_make_window (window->node);
  1347.   active_window = old_active;
  1348.  
  1349.   if (!split)
  1350.     {
  1351.       info_error (WIN_TOO_SMALL);
  1352.     }
  1353.   else
  1354.     {
  1355. #if defined (SPLIT_BEFORE_ACTIVE)
  1356.       /* Try to scroll the old window into its new postion. */
  1357.       if (pagetop == window->pagetop)
  1358.     {
  1359.       int start, end, amount;
  1360.  
  1361.       start = split->first_row;
  1362.       end = start + window->height;
  1363.       amount = split->height + 1;
  1364.       display_scroll_display (start, end, amount);
  1365.     }
  1366. #else /* !SPLIT_BEFORE_ACTIVE */
  1367.       /* Make sure point still appears in the active window. */
  1368.       info_show_point (window);
  1369. #endif /* !SPLIT_BEFORE_ACTIVE */
  1370.  
  1371.       /* If the window just split was one internal to Info, try to display
  1372.      something else in it. */
  1373.       if (internal_info_node_p (split->node))
  1374.     {
  1375.       register int i, j;
  1376.       INFO_WINDOW *iw;
  1377.       NODE *node = (NODE *)NULL;
  1378.       char *filename;
  1379.  
  1380.       for (i = 0; iw = info_windows[i]; i++)
  1381.         {
  1382.           for (j = 0; j < iw->nodes_index; j++)
  1383.         if (!internal_info_node_p (iw->nodes[j]))
  1384.           {
  1385.             if (iw->nodes[j]->parent)
  1386.               filename = iw->nodes[j]->parent;
  1387.             else
  1388.               filename = iw->nodes[j]->filename;
  1389.  
  1390.             node = info_get_node (filename, iw->nodes[j]->nodename);
  1391.             if (node)
  1392.               {
  1393.             window_set_node_of_window (split, node);
  1394.             i = info_windows_index - 1;
  1395.             break;
  1396.               }
  1397.           }
  1398.         }
  1399.     }
  1400.       split->pagetop = window->pagetop;
  1401.  
  1402.       if (auto_tiling_p)
  1403.     window_tile_windows (DONT_TILE_INTERNALS);
  1404.       else
  1405.     window_adjust_pagetop (split);
  1406.  
  1407.       remember_window_and_node (split, split->node);
  1408.     }
  1409. }
  1410.  
  1411. /* Delete WINDOW, forgetting the list of last visited nodes.  If we are
  1412.    automatically displaying footnotes, show or remove the footnotes
  1413.    window.  If we are automatically tiling windows, re-tile after the
  1414.    deletion. */
  1415. DECLARE_INFO_COMMAND (info_delete_window, "Delete the current window")
  1416. {
  1417.   if (!windows->next)
  1418.     {
  1419.       info_error (CANT_KILL_LAST);
  1420.     }
  1421.   else if (window->flags & W_WindowIsPerm)
  1422.     {
  1423.       info_error ("Cannot delete a permanent window");
  1424.     }
  1425.   else
  1426.     {
  1427.       info_delete_window_internal (window);
  1428.  
  1429.       if (auto_footnotes_p)
  1430.     info_get_or_remove_footnotes (active_window);
  1431.  
  1432.       if (auto_tiling_p)
  1433.     window_tile_windows (DONT_TILE_INTERNALS);
  1434.     }
  1435. }
  1436.  
  1437. /* Do the physical deletion of WINDOW, and forget this window and
  1438.    associated nodes. */
  1439. void
  1440. info_delete_window_internal (window)
  1441.      WINDOW *window;
  1442. {
  1443.   if (windows->next && ((window->flags & W_WindowIsPerm) == 0))
  1444.     {
  1445.       /* We not only delete the window from the display, we forget it from
  1446.      our list of remembered windows. */
  1447.       forget_window_and_nodes (window);
  1448.       window_delete_window (window);
  1449.  
  1450.       if (echo_area_is_active)
  1451.     echo_area_inform_of_deleted_window (window);
  1452.     }
  1453. }
  1454.  
  1455. /* Just keep WINDOW, deleting all others. */
  1456. DECLARE_INFO_COMMAND (info_keep_one_window, "Delete all other windows")
  1457. {
  1458.   int num_deleted;        /* The number of windows we deleted. */
  1459.   int pagetop, start, end;
  1460.  
  1461.   /* Remember a few things about this window.  We may be able to speed up
  1462.      redisplay later by scrolling its contents. */
  1463.   pagetop = window->pagetop;
  1464.   start = window->first_row;
  1465.   end = start + window->height;
  1466.  
  1467.   num_deleted = 0;
  1468.  
  1469.   while (1)
  1470.     {
  1471.       WINDOW *win;
  1472.  
  1473.       /* Find an eligible window and delete it.  If no eligible windows
  1474.      are found, we are done.  A window is eligible for deletion if
  1475.      is it not permanent, and it is not WINDOW. */
  1476.       for (win = windows; win; win = win->next)
  1477.     if (win != window && ((win->flags & W_WindowIsPerm) == 0))
  1478.       break;
  1479.  
  1480.       if (!win)
  1481.     break;
  1482.  
  1483.       info_delete_window_internal (win);
  1484.       num_deleted++;
  1485.     }
  1486.  
  1487.   /* Scroll the contents of this window into the right place so that the
  1488.      user doesn't have to wait any longer than necessary for redisplay. */
  1489.   if (num_deleted)
  1490.     {
  1491.       int amount;
  1492.  
  1493.       amount = (window->first_row - start);
  1494.       amount -= (window->pagetop - pagetop);
  1495.       display_scroll_display (start, end, amount);
  1496.     }
  1497.  
  1498.   window->flags |= W_UpdateWindow;
  1499. }
  1500.  
  1501. /* Scroll the "other" window of WINDOW. */
  1502. DECLARE_INFO_COMMAND (info_scroll_other_window, "Scroll the other window")
  1503. {
  1504.   WINDOW *other;
  1505.  
  1506.   /* If only one window, give up. */
  1507.   if (!windows->next)
  1508.     {
  1509.       info_error (ONE_WINDOW);
  1510.       return;
  1511.     }
  1512.  
  1513.   other = window->next;
  1514.  
  1515.   if (!other)
  1516.     other = window->prev;
  1517.  
  1518.   info_scroll_forward (other, count, key);
  1519. }
  1520.  
  1521. /* Change the size of WINDOW by AMOUNT. */
  1522. DECLARE_INFO_COMMAND (info_grow_window, "Grow (or shrink) this window")
  1523. {
  1524.   window_change_window_height (window, count);
  1525. }
  1526.  
  1527. /* When non-zero, tiling takes place automatically when info_split_window
  1528.    is called. */
  1529. int auto_tiling_p = 0;
  1530.  
  1531. /* Tile all of the visible windows. */
  1532. DECLARE_INFO_COMMAND (info_tile_windows,
  1533.     "Divide the available screen space among the visible windows")
  1534. {
  1535.   window_tile_windows (TILE_INTERNALS);
  1536. }
  1537.  
  1538. /* Toggle the state of this window's wrapping of lines. */
  1539. DECLARE_INFO_COMMAND (info_toggle_wrap,
  1540.           "Toggle the state of line wrapping in the current window")
  1541. {
  1542.   window_toggle_wrap (window);
  1543. }
  1544.  
  1545. /* **************************************************************** */
  1546. /*                                    */
  1547. /*            Info Node Commands                */
  1548. /*                                    */
  1549. /* **************************************************************** */
  1550.  
  1551. /* Using WINDOW for various defaults, select the node referenced by ENTRY
  1552.    in it.  If the node is selected, the window and node are remembered. */
  1553. void
  1554. info_select_reference (window, entry)
  1555.      WINDOW *window;
  1556.      REFERENCE *entry;
  1557. {
  1558.   NODE *node;
  1559.   char *filename, *nodename, *file_system_error;
  1560.  
  1561.   file_system_error = (char *)NULL;
  1562.  
  1563.   filename = entry->filename;
  1564.   if (!filename)
  1565.     filename = window->node->parent;
  1566.   if (!filename)
  1567.     filename = window->node->filename;
  1568.  
  1569.   if (filename)
  1570.     filename = savestring (filename);
  1571.  
  1572.   if (entry->nodename)
  1573.     nodename = savestring (entry->nodename);
  1574.   else
  1575.     nodename = savestring ("Top");
  1576.  
  1577.   node = info_get_node (filename, nodename);
  1578.  
  1579.   /* Try something a little weird.  If the node couldn't be found, and the
  1580.      reference was of the form "foo::", see if the entry->label can be found
  1581.      as a file, with a node of "Top". */
  1582.   if (!node)
  1583.     {
  1584.       if (info_recent_file_error)
  1585.     file_system_error = savestring (info_recent_file_error);
  1586.  
  1587.       if (entry->nodename && (strcmp (entry->nodename, entry->label) == 0))
  1588.     {
  1589.       node = info_get_node (entry->label, "Top");
  1590.       if (!node && info_recent_file_error)
  1591.         {
  1592.           maybe_free (file_system_error);
  1593.           file_system_error = savestring (info_recent_file_error);
  1594.         }
  1595.     }
  1596.     }
  1597.  
  1598.   if (!node)
  1599.     {
  1600.       if (file_system_error)
  1601.     info_error (file_system_error);
  1602.       else
  1603.     info_error (CANT_FIND_NODE, nodename);
  1604.     }
  1605.  
  1606.   maybe_free (file_system_error);
  1607.   maybe_free (filename);
  1608.   maybe_free (nodename);
  1609.  
  1610.   if (node)
  1611.     {
  1612.       set_remembered_pagetop_and_point (window);
  1613.       info_set_node_of_window (window, node);
  1614.     }
  1615. }
  1616.  
  1617. /* Parse the node specification in LINE using WINDOW to default the filename.
  1618.    Select the parsed node in WINDOW and remember it, or error if the node
  1619.    couldn't be found. */
  1620. static void
  1621. info_parse_and_select (line, window)
  1622.      char *line;
  1623.      WINDOW *window;
  1624. {
  1625.   REFERENCE entry;
  1626.  
  1627.   info_parse_node (line, DONT_SKIP_NEWLINES);
  1628.  
  1629.   entry.nodename = info_parsed_nodename;
  1630.   entry.filename = info_parsed_filename;
  1631.   entry.label = "*info-parse-and-select*";
  1632.  
  1633.   info_select_reference (window, &entry);
  1634. }
  1635.  
  1636. /* Given that the values of INFO_PARSED_FILENAME and INFO_PARSED_NODENAME
  1637.    are previously filled, try to get the node represented by them into
  1638.    WINDOW.  The node should have been pointed to by the LABEL pointer of
  1639.    WINDOW->node. */
  1640. static void
  1641. info_handle_pointer (label, window)
  1642.      char *label;
  1643.      WINDOW *window;
  1644. {
  1645.   if (info_parsed_filename || info_parsed_nodename)
  1646.     {
  1647.       char *filename, *nodename;
  1648.       NODE *node;
  1649.  
  1650.       filename = nodename = (char *)NULL;
  1651.  
  1652.       if (info_parsed_filename)
  1653.     filename = savestring (info_parsed_filename);
  1654.       else
  1655.     {
  1656.       if (window->node->parent)
  1657.         filename = savestring (window->node->parent);
  1658.       else if (window->node->filename)
  1659.         filename = savestring (window->node->filename);
  1660.     }
  1661.  
  1662.       if (info_parsed_nodename)
  1663.     nodename = savestring (info_parsed_nodename);
  1664.       else
  1665.     nodename = savestring ("Top");
  1666.  
  1667.       node = info_get_node (filename, nodename);
  1668.  
  1669.       if (node)
  1670.     {
  1671.       INFO_WINDOW *info_win;
  1672.  
  1673.       info_win = get_info_window_of_window (window);
  1674.       if (info_win)
  1675.         {
  1676.           info_win->pagetops[info_win->current] = window->pagetop;
  1677.           info_win->points[info_win->current] = window->point;
  1678.         }
  1679.       set_remembered_pagetop_and_point (window);
  1680.       info_set_node_of_window (window, node);
  1681.     }
  1682.       else
  1683.     {
  1684.       if (info_recent_file_error)
  1685.         info_error (info_recent_file_error);
  1686.       else
  1687.         info_error (CANT_FILE_NODE, filename, nodename);
  1688.     }
  1689.  
  1690.       free (filename);
  1691.       free (nodename);
  1692.     }
  1693.   else
  1694.     {
  1695.       info_error (NO_POINTER, label);
  1696.     }
  1697. }
  1698.  
  1699. /* Make WINDOW display the "Next:" node of the node currently being
  1700.    displayed. */
  1701. DECLARE_INFO_COMMAND (info_next_node, "Select the `Next' node")
  1702. {
  1703.   info_next_label_of_node (window->node);
  1704.   info_handle_pointer ("Next", window);
  1705. }
  1706.  
  1707. /* Make WINDOW display the "Prev:" node of the node currently being
  1708.    displayed. */
  1709. DECLARE_INFO_COMMAND (info_prev_node, "Select the `Prev' node")
  1710. {
  1711.   info_prev_label_of_node (window->node);
  1712.   info_handle_pointer ("Prev", window);
  1713. }
  1714.  
  1715. /* Make WINDOW display the "Up:" node of the node currently being
  1716.    displayed. */
  1717. DECLARE_INFO_COMMAND (info_up_node, "Select the `Up' node")
  1718. {
  1719.   info_up_label_of_node (window->node);
  1720.   info_handle_pointer ("Up", window);
  1721. }
  1722.  
  1723. /* Make WINDOW display the last node of this info file. */
  1724. DECLARE_INFO_COMMAND (info_last_node, "Select the last node in this file")
  1725. {
  1726.   register int i;
  1727.   FILE_BUFFER *fb = file_buffer_of_window (window);
  1728.   NODE *node = (NODE *)NULL;
  1729.  
  1730.   if (fb && fb->tags)
  1731.     {
  1732.       for (i = 0; fb->tags[i]; i++);
  1733.       node = info_get_node (fb->filename, fb->tags[i - 1]->nodename);
  1734.     }
  1735.  
  1736.   if (!node)
  1737.     info_error ("This window has no additional nodes");
  1738.   else
  1739.     {
  1740.       set_remembered_pagetop_and_point (window);
  1741.       info_set_node_of_window (window, node);
  1742.     }
  1743. }
  1744.  
  1745. /* Make WINDOW display the first node of this info file. */
  1746. DECLARE_INFO_COMMAND (info_first_node, "Select the first node in this file")
  1747. {
  1748.   FILE_BUFFER *fb = file_buffer_of_window (window);
  1749.   NODE *node = (NODE *)NULL;
  1750.  
  1751.   if (fb && fb->tags)
  1752.     node = info_get_node (fb->filename, fb->tags[0]->nodename);
  1753.  
  1754.   if (!node)
  1755.     info_error ("This window has no additional nodes");
  1756.   else
  1757.     {
  1758.       set_remembered_pagetop_and_point (window);
  1759.       info_set_node_of_window (window, node);
  1760.     }
  1761. }
  1762.  
  1763. /* Make WINDOW display the previous node displayed in this window. */
  1764. DECLARE_INFO_COMMAND (info_history_node,
  1765.               "Select the most recently selected node")
  1766. {
  1767.   INFO_WINDOW *info_win;
  1768.  
  1769.   /* Find the INFO_WINDOW which contains WINDOW. */
  1770.   info_win = get_info_window_of_window (window);
  1771.  
  1772.   if (!info_win)
  1773.     {
  1774.       info_error ("Requested window is not present!");
  1775.       return;
  1776.     }
  1777.  
  1778.   set_remembered_pagetop_and_point (window);
  1779.   if (!info_win->current)
  1780.     {
  1781.       if (info_win->nodes_index > 1)
  1782.     {
  1783.       window_message_in_echo_area
  1784.         ("Now wrapped around to beginning of history.");
  1785.       info_win->current = info_win->nodes_index;
  1786.     }
  1787.       else
  1788.     {
  1789.       info_error ("No earlier nodes in this window.");
  1790.       return;
  1791.     }
  1792.     }
  1793.  
  1794.   info_win->current--;
  1795.   window_set_node_of_window (window, info_win->nodes[info_win->current]);
  1796.   window->pagetop = info_win->pagetops[info_win->current];
  1797.   window->point   = info_win->points[info_win->current];
  1798.   window->flags |= W_UpdateWindow;
  1799.   if (auto_footnotes_p)
  1800.     info_get_or_remove_footnotes (window);
  1801. }
  1802.  
  1803. /* Select the last menu item in WINDOW->node. */
  1804. DECLARE_INFO_COMMAND (info_last_menu_item,
  1805.    "Select the last item in this node's menu")
  1806. {
  1807.   info_menu_digit (window, 1, '0');
  1808. }
  1809.  
  1810. /* Use KEY (a digit) to select the Nth menu item in WINDOW->node. */
  1811. DECLARE_INFO_COMMAND (info_menu_digit, "Select this menu item")
  1812. {
  1813.   register int i, item;
  1814.   register REFERENCE *entry, **menu;
  1815.  
  1816.   menu = info_menu_of_node (window->node);
  1817.  
  1818.   if (!menu)
  1819.     {
  1820.       info_error (NO_MENU_NODE);
  1821.       return;
  1822.     }
  1823.  
  1824.   /* We have the menu.  See if there are this many items in it. */
  1825.   item = key - '0';
  1826.  
  1827.   /* Special case.  Item "0" is the last item in this menu. */
  1828.   if (item == 0)
  1829.     for (i = 0; menu[i + 1]; i++);
  1830.   else
  1831.     {
  1832.       for (i = 0; entry = menu[i]; i++)
  1833.     if (i == item - 1)
  1834.       break;
  1835.     }
  1836.  
  1837.   if (menu[i])
  1838.     info_select_reference (window, menu[i]);
  1839.   else
  1840.     info_error ("There aren't %d items in this menu.", item);
  1841.  
  1842.   info_free_references (menu);
  1843.   return;
  1844. }
  1845.  
  1846. /* Read a menu or followed reference from the user defaulting to the
  1847.    reference found on the current line, and select that node.  The
  1848.    reading is done with completion.  BUILDER is the function used
  1849.    to build the list of references.  ASK_P is non-zero if the user
  1850.    should be prompted, or zero to select the default item. */
  1851. static void
  1852. info_menu_or_ref_item (window, count, key, builder, ask_p)
  1853.      WINDOW *window;
  1854.      int count;
  1855.      unsigned char key;
  1856.      REFERENCE **(*builder) ();
  1857.      int ask_p;
  1858. {
  1859.   REFERENCE **menu, *entry, *defentry = (REFERENCE *)NULL;
  1860.   char *line;
  1861.  
  1862.   menu = (*builder) (window->node);
  1863.  
  1864.   if (!menu)
  1865.     {
  1866.       if (builder == info_menu_of_node)
  1867.     info_error (NO_MENU_NODE);
  1868.       else
  1869.     info_error (NO_XREF_NODE);
  1870.       return;
  1871.     }
  1872.  
  1873.   /* Default the selected reference to the one which is on the line that
  1874.      point is in.  */
  1875.   {
  1876.     REFERENCE **refs = (REFERENCE **)NULL;
  1877.     int point_line;
  1878.  
  1879.     point_line = window_line_of_point (window);
  1880.  
  1881.     if (point_line != -1)
  1882.       {
  1883.     SEARCH_BINDING binding;
  1884.  
  1885.     binding.start = 0;
  1886.     binding.buffer = window->line_starts[point_line];
  1887.     if (window->line_starts[point_line + 1])
  1888.       binding.end = window->line_starts[point_line + 1] - binding.buffer;
  1889.     else
  1890.       binding.end =
  1891.         (window->node->contents + window->node->nodelen) - binding.buffer;
  1892.     binding.flags = 0;
  1893.  
  1894.     if (builder == info_menu_of_node)
  1895.       {
  1896.         if (point_line)
  1897.           {
  1898.         binding.buffer--;
  1899.         binding.end++;
  1900.  
  1901.         refs = info_menu_items (&binding);
  1902.           }
  1903.       }
  1904.     else
  1905.       refs = info_xrefs (&binding);
  1906.  
  1907.     if (refs)
  1908.       {
  1909.         if ((strcmp (refs[0]->label, "Menu") != 0) ||
  1910.         (builder == info_xrefs_of_node))
  1911.           {
  1912.         defentry = (REFERENCE *)xmalloc (sizeof (REFERENCE));
  1913.         defentry->label = savestring (refs[0]->label);
  1914.         defentry->filename = refs[0]->filename;
  1915.         defentry->nodename = refs[0]->nodename;
  1916.  
  1917.         if (defentry->filename)
  1918.           defentry->filename = savestring (defentry->filename);
  1919.         if (defentry->nodename)
  1920.           defentry->nodename = savestring (defentry->nodename);
  1921.           }
  1922.         info_free_references (refs);
  1923.       }
  1924.       }
  1925.   }
  1926.  
  1927.   /* If we are going to ask the user a question, do it now. */
  1928.   if (ask_p)
  1929.     {
  1930.       char *prompt;
  1931.  
  1932.       /* Build the prompt string. */
  1933.       if (defentry)
  1934.     prompt = (char *)xmalloc (20 + strlen (defentry->label));
  1935.       else
  1936.     prompt = (char *)xmalloc (20);
  1937.  
  1938.       if (builder == info_menu_of_node)
  1939.     {
  1940.       if (defentry)
  1941.         sprintf (prompt, "Menu item (%s): ", defentry->label);
  1942.       else
  1943.         sprintf (prompt, "Menu item: ");
  1944.     }
  1945.       else
  1946.     {
  1947.       if (defentry)
  1948.         sprintf (prompt, "Follow xref (%s): ", defentry->label);
  1949.       else
  1950.         sprintf (prompt, "Follow xref: ");
  1951.     }
  1952.  
  1953.       line = info_read_completing_in_echo_area (window, prompt, menu);
  1954.       free (prompt);
  1955.  
  1956.       window = active_window;
  1957.  
  1958.       /* User aborts, just quit. */
  1959.       if (!line)
  1960.     {
  1961.       maybe_free (defentry);
  1962.       info_free_references (menu);
  1963.       info_abort_key (window, 0, 0);
  1964.       return;
  1965.     }
  1966.  
  1967.       /* If we had a default and the user accepted it, use that. */
  1968.       if (!*line)
  1969.     {
  1970.       free (line);
  1971.       if (defentry)
  1972.         line = savestring (defentry->label);
  1973.       else
  1974.         line = (char *)NULL;
  1975.     }
  1976.     }
  1977.   else
  1978.     {
  1979.       /* Not going to ask any questions.  If we have a default entry, use
  1980.      that, otherwise return. */
  1981.       if (!defentry)
  1982.     return;
  1983.       else
  1984.     line = savestring (defentry->label);
  1985.     }
  1986.  
  1987.   if (line)
  1988.     {
  1989.       /* Find the selected label in the references. */
  1990.       entry = info_get_labeled_reference (line, menu);
  1991.  
  1992.       if (!entry && defentry)
  1993.     info_error ("The reference disappeared! (%s).", line);
  1994.       else
  1995.     {
  1996.       NODE *orig;
  1997.  
  1998.       orig = window->node;
  1999.       info_select_reference (window, entry);
  2000.       if ((builder == info_xrefs_of_node) && (window->node != orig))
  2001.         {
  2002.           long offset;
  2003.           long start;
  2004.  
  2005.           if (window->line_count > 0)
  2006.         start = window->line_starts[1] - window->node->contents;
  2007.           else
  2008.         start = 0;
  2009.  
  2010.           offset =
  2011.         info_target_search_node (window->node, entry->label, start);
  2012.  
  2013.           if (offset != -1)
  2014.         {
  2015.           window->point = offset;
  2016.           window_adjust_pagetop (window);
  2017.         }
  2018.         }
  2019.     }
  2020.  
  2021.       free (line);
  2022.       if (defentry)
  2023.     {
  2024.       free (defentry->label);
  2025.       maybe_free (defentry->filename);
  2026.       maybe_free (defentry->nodename);
  2027.       free (defentry);
  2028.     }
  2029.     }
  2030.  
  2031.   info_free_references (menu);
  2032.  
  2033.   if (!info_error_was_printed)
  2034.     window_clear_echo_area ();
  2035. }
  2036.  
  2037. /* Read a line (with completion) which is the name of a menu item,
  2038.    and select that item. */
  2039. DECLARE_INFO_COMMAND (info_menu_item, "Read a menu item and select its node")
  2040. {
  2041.   info_menu_or_ref_item (window, count, key, info_menu_of_node, 1);
  2042. }
  2043.  
  2044. /* Read a line (with completion) which is the name of a reference to
  2045.    follow, and select the node. */
  2046. DECLARE_INFO_COMMAND
  2047.   (info_xref_item, "Read a footnote or cross reference and select its node")
  2048. {
  2049.   info_menu_or_ref_item (window, count, key, info_xrefs_of_node, 1);
  2050. }
  2051.  
  2052. /* Position the cursor at the start of this node's menu. */
  2053. DECLARE_INFO_COMMAND (info_find_menu, "Move to the start of this node's menu")
  2054. {
  2055.   SEARCH_BINDING binding;
  2056.   long position;
  2057.  
  2058.   binding.buffer = window->node->contents;
  2059.   binding.start  = 0;
  2060.   binding.end = window->node->nodelen;
  2061.   binding.flags = S_FoldCase | S_SkipDest;
  2062.  
  2063.   position = search (INFO_MENU_LABEL, &binding);
  2064.  
  2065.   if (position == -1)
  2066.     info_error (NO_MENU_NODE);
  2067.   else
  2068.     {
  2069.       window->point = position;
  2070.       window_adjust_pagetop (window);
  2071.       window->flags |= W_UpdateWindow;
  2072.     }
  2073. }
  2074.  
  2075. /* Visit as many menu items as is possible, each in a separate window. */
  2076. DECLARE_INFO_COMMAND (info_visit_menu,
  2077.   "Visit as many menu items at once as possible")
  2078. {
  2079.   register int i;
  2080.   REFERENCE *entry, **menu;
  2081.  
  2082.   menu = info_menu_of_node (window->node);
  2083.  
  2084.   if (!menu)
  2085.     info_error (NO_MENU_NODE);
  2086.  
  2087.   for (i = 0; (!info_error_was_printed) && (entry = menu[i]); i++)
  2088.     {
  2089.       WINDOW *new;
  2090.  
  2091.       new = window_make_window (window->node);
  2092.       window_tile_windows (TILE_INTERNALS);
  2093.  
  2094.       if (!new)
  2095.     info_error (WIN_TOO_SMALL);
  2096.       else
  2097.     {
  2098.       active_window = new;
  2099.       info_select_reference (new, entry);
  2100.     }
  2101.     }
  2102. }
  2103.  
  2104. /* Read a line of input which is a node name, and go to that node. */
  2105. DECLARE_INFO_COMMAND (info_goto_node, "Read a node name and select it")
  2106. {
  2107.   char *line;
  2108.   NODE *node;
  2109.  
  2110. #define GOTO_COMPLETES
  2111. #if defined (GOTO_COMPLETES)
  2112.   /* Build a completion list of all of the known nodes. */
  2113.   {
  2114.     register int fbi, i;
  2115.     FILE_BUFFER *current;
  2116.     REFERENCE **items = (REFERENCE **)NULL;
  2117.     int items_index = 0;
  2118.     int items_slots = 0;
  2119.  
  2120.     current = file_buffer_of_window (window);
  2121.  
  2122.     for (fbi = 0; info_loaded_files && info_loaded_files[fbi]; fbi++)
  2123.       {
  2124.     FILE_BUFFER *fb;
  2125.     REFERENCE *entry;
  2126.     int this_is_the_current_fb;
  2127.  
  2128.     fb = info_loaded_files[fbi];
  2129.     this_is_the_current_fb = (current == fb);
  2130.  
  2131.     entry = (REFERENCE *)xmalloc (sizeof (REFERENCE));
  2132.     entry->filename = entry->nodename = (char *)NULL;
  2133.     entry->label = (char *)xmalloc (4 + strlen (fb->filename));
  2134.     sprintf (entry->label, "(%s)*", fb->filename);
  2135.  
  2136.     add_pointer_to_array
  2137.       (entry, items_index, items, items_slots, 10, REFERENCE *);
  2138.  
  2139.     if (fb->tags)
  2140.       {
  2141.         for (i = 0; fb->tags[i]; i++)
  2142.           {
  2143.         entry = (REFERENCE *)xmalloc (sizeof (REFERENCE));
  2144.         entry->filename = entry->nodename = (char *)NULL;
  2145.         entry->label = (char *) xmalloc
  2146.           (4 + strlen (fb->filename) + strlen (fb->tags[i]->nodename));
  2147.         sprintf (entry->label, "(%s)%s",
  2148.              fb->filename, fb->tags[i]->nodename);
  2149.  
  2150.         add_pointer_to_array
  2151.           (entry, items_index, items, items_slots, 100, REFERENCE *);
  2152.           }        
  2153.  
  2154.         if (this_is_the_current_fb)
  2155.           {
  2156.         for (i = 0; fb->tags[i]; i++)
  2157.           {
  2158.             entry = (REFERENCE *)xmalloc (sizeof (REFERENCE));
  2159.             entry->filename = entry->nodename = (char *)NULL;
  2160.             entry->label = savestring (fb->tags[i]->nodename);
  2161.             add_pointer_to_array (entry, items_index, items,
  2162.                       items_slots, 100, REFERENCE *);
  2163.           }
  2164.           }
  2165.       }
  2166.       }
  2167.     line = info_read_maybe_completing (window, "Goto Node: ", items);
  2168.     info_free_references (items);
  2169.   }
  2170. #else /* !GOTO_COMPLETES */
  2171.   line = info_read_in_echo_area (window, "Goto Node: ");
  2172. #endif /* !GOTO_COMPLETES */
  2173.  
  2174.   /* If the user aborted, quit now. */
  2175.   if (!line)
  2176.     {
  2177.       info_abort_key (window, 0, 0);
  2178.       return;
  2179.     }
  2180.  
  2181.   canonicalize_whitespace (line);
  2182.  
  2183.   if (*line)
  2184.     info_parse_and_select (line, window);
  2185.  
  2186.   free (line);
  2187.   if (!info_error_was_printed)
  2188.     window_clear_echo_area ();
  2189. }
  2190.  
  2191. /* Move to the "Top" node in this file. */
  2192. DECLARE_INFO_COMMAND (info_top_node, "Select the node `Top' in this file")
  2193. {
  2194.   info_parse_and_select ("Top", window);
  2195. }
  2196.  
  2197. /* Move to the node "(dir)Top". */
  2198. DECLARE_INFO_COMMAND (info_dir_node, "Select the node `(dir)'")
  2199. {
  2200.   info_parse_and_select ("(dir)Top", window);
  2201. }
  2202.  
  2203. /* Try to delete the current node appearing in this window, showing the most
  2204.    recently selected node in this window. */
  2205. DECLARE_INFO_COMMAND (info_kill_node, "Kill this node")
  2206. {
  2207.   register int iw, i;
  2208.   register INFO_WINDOW *info_win;
  2209.   char *nodename = (char *)NULL;
  2210.   NODE *temp = (NODE *)NULL;
  2211.  
  2212.   /* Read the name of a node to kill.  The list of available nodes comes
  2213.      from the nodes appearing in the current window configuration. */
  2214.   {
  2215.     REFERENCE **menu = (REFERENCE **)NULL;
  2216.     int menu_index = 0, menu_slots = 0;
  2217.     char *default_nodename, *prompt;
  2218.  
  2219.     for (iw = 0; info_win = info_windows[iw]; iw++)
  2220.       {
  2221.     REFERENCE *entry;
  2222.  
  2223.     entry = (REFERENCE *)xmalloc (sizeof (REFERENCE));
  2224.     entry->label = savestring (info_win->window->node->nodename);
  2225.     entry->filename = entry->nodename = (char *)NULL;
  2226.  
  2227.     add_pointer_to_array
  2228.       (entry, menu_index, menu, menu_slots, 10, REFERENCE *);
  2229.       }
  2230.  
  2231.     default_nodename = savestring (active_window->node->nodename);
  2232.     prompt = (char *)xmalloc (40 + strlen (default_nodename));
  2233.     sprintf (prompt, "Kill node (%s): ", default_nodename);
  2234.  
  2235.     nodename = info_read_completing_in_echo_area (window, prompt, menu);
  2236.     free (prompt);
  2237.     info_free_references (menu);
  2238.     if (nodename && !*nodename)
  2239.       {
  2240.     free (nodename);
  2241.     nodename = default_nodename;
  2242.       }
  2243.     else
  2244.       free (default_nodename);
  2245.   }
  2246.  
  2247.   /* If there is no nodename to kill, quit now. */
  2248.   if (!nodename)
  2249.     {
  2250.       info_abort_key (window, 0, 0);
  2251.       return;
  2252.     }
  2253.  
  2254.   /* If there is a nodename, find it in our window list. */
  2255.   for (iw = 0; info_win = info_windows[iw]; iw++)
  2256.     if (strcmp (nodename, info_win->nodes[info_win->current]->nodename) == 0)
  2257.       break;
  2258.  
  2259.   if (!info_win)
  2260.     {
  2261.       if (*nodename)
  2262.     info_error ("Cannot kill the node `%s'", nodename);
  2263.       else
  2264.     window_clear_echo_area ();
  2265.  
  2266.       return;
  2267.     }
  2268.  
  2269.   /* If there are no more nodes left anywhere to view, complain and exit. */
  2270.   if (info_windows_index == 1 && info_windows[0]->nodes_index == 1)
  2271.     {
  2272.       info_error ("Cannot kill the last node");
  2273.       return;
  2274.     }
  2275.  
  2276.   /* INFO_WIN contains the node that the user wants to stop viewing.
  2277.      Delete this node from the list of nodes previously shown in this
  2278.      window. */
  2279.   for (i = info_win->current; i < info_win->nodes_index; i++)
  2280.     info_win->nodes[i] = info_win->nodes[i++];
  2281.  
  2282.   /* There is one less node in this window's history list. */
  2283.   info_win->nodes_index--;
  2284.  
  2285.   /* Make this window show the most recent history node. */
  2286.   info_win->current = info_win->nodes_index - 1;
  2287.  
  2288.   /* If there aren't any nodes left in this window, steal one from the
  2289.      next window. */
  2290.   if (info_win->current < 0)
  2291.     {
  2292.       INFO_WINDOW *stealer;
  2293.       int which, pagetop;
  2294.       long point;
  2295.  
  2296.       if (info_windows[iw + 1])
  2297.     stealer = info_windows[iw + 1];
  2298.       else
  2299.     stealer = info_windows[0];
  2300.  
  2301.       /* If the node being displayed in the next window is not the most
  2302.      recently loaded one, get the most recently loaded one. */
  2303.       if ((stealer->nodes_index - 1) != stealer->current)
  2304.     which = stealer->nodes_index - 1;
  2305.  
  2306.       /* Else, if there is another node behind the stealers current node,
  2307.      use that one. */
  2308.       else if (stealer->current > 0)
  2309.     which = stealer->current - 1;
  2310.  
  2311.       /* Else, just use the node appearing in STEALER's window. */
  2312.       else
  2313.     which = stealer->current;
  2314.  
  2315.       /* Copy this node. */
  2316.       {
  2317.     NODE *copy;
  2318.  
  2319.     temp = stealer->nodes[which];
  2320.     point = stealer->points[which];
  2321.     pagetop = stealer->pagetops[which];
  2322.  
  2323.     copy = (NODE *)xmalloc (sizeof (NODE));
  2324.     copy->filename = temp->filename;
  2325.     copy->parent = temp->parent;
  2326.     copy->nodename = temp->nodename;
  2327.     copy->contents = temp->contents;
  2328.     copy->nodelen = temp->nodelen;
  2329.     copy->flags = temp->flags;
  2330.  
  2331.     temp = copy;
  2332.       }
  2333.  
  2334.       window_set_node_of_window (info_win->window, temp);
  2335.       window->point = point;
  2336.       window->pagetop = pagetop;
  2337.       remember_window_and_node (info_win->window, temp);
  2338.     }
  2339.   else
  2340.     {
  2341.       temp = info_win->nodes[info_win->current];
  2342.       window_set_node_of_window (info_win->window, temp);
  2343.     }
  2344.   if (!info_error_was_printed)
  2345.     window_clear_echo_area ();
  2346. }
  2347.  
  2348. /* Read the name of a file and select the entire file. */
  2349. DECLARE_INFO_COMMAND (info_view_file, "Read the name of a file and select it")
  2350. {
  2351.   char *line;
  2352.  
  2353.   line = info_read_in_echo_area (window, "Find file: ");
  2354.   if (!line)
  2355.     {
  2356.       info_abort_key (active_window, 1, 0);
  2357.       return;
  2358.     }
  2359.  
  2360.   if (*line)
  2361.     {
  2362.       NODE *node;
  2363.  
  2364.       node = info_get_node (line, "*");
  2365.       if (!node)
  2366.     {
  2367.       if (info_recent_file_error)
  2368.         info_error (info_recent_file_error);
  2369.       else
  2370.         info_error ("Cannot find \"%s\".", line);
  2371.     }
  2372.       else
  2373.     {
  2374.       set_remembered_pagetop_and_point (active_window);
  2375.       info_set_node_of_window (window, node);
  2376.     }
  2377.       free (line);
  2378.     }
  2379.  
  2380.   if (!info_error_was_printed)
  2381.     window_clear_echo_area ();
  2382. }
  2383.  
  2384. /* **************************************************************** */
  2385. /*                                    */
  2386. /*           Dumping and Printing Nodes                */
  2387. /*                                    */
  2388. /* **************************************************************** */
  2389.  
  2390. #define VERBOSE_NODE_DUMPING
  2391. static void write_node_to_stream ();
  2392. static void dump_node_to_stream ();
  2393. static void initialize_dumping ();
  2394.  
  2395. /* Dump the nodes specified by FILENAME and NODENAMES to the file named
  2396.    in OUTPUT_FILENAME.  If DUMP_SUBNODES is non-zero, recursively dump
  2397.    the nodes which appear in the menu of each node dumped. */
  2398. void
  2399. dump_nodes_to_file (filename, nodenames, output_filename, dump_subnodes)
  2400.      char *filename;
  2401.      char **nodenames;
  2402.      char *output_filename;
  2403.      int dump_subnodes;
  2404. {
  2405.   register int i;
  2406.   FILE *output_stream;
  2407.  
  2408.   /* Get the stream to print the nodes to.  Special case of an output
  2409.      filename of "-" means to dump the nodes to stdout. */
  2410.   if (strcmp (output_filename, "-") == 0)
  2411.     output_stream = stdout;
  2412.   else
  2413.     output_stream = fopen (output_filename, "w");
  2414.  
  2415.   if (!output_stream)
  2416.     {
  2417.       info_error ("Could not create output file \"%s\".", output_filename);
  2418.       return;
  2419.     }
  2420.  
  2421.   /* Print each node to stream. */
  2422.   initialize_dumping ();
  2423.   for (i = 0; nodenames[i]; i++)
  2424.     dump_node_to_stream (filename, nodenames[i], output_stream, dump_subnodes);
  2425.  
  2426.   if (output_stream != stdout)
  2427.     fclose (output_stream);
  2428.  
  2429. #if defined (VERBOSE_NODE_DUMPING)
  2430.   info_error ("Done.");
  2431. #endif /* VERBOSE_NODE_DUMPING */
  2432. }
  2433.  
  2434. /* A place to remember already dumped nodes. */
  2435. static char **dumped_already = (char **)NULL;
  2436. static int dumped_already_index = 0;
  2437. static int dumped_already_slots = 0;
  2438.  
  2439. static void
  2440. initialize_dumping ()
  2441. {
  2442.   dumped_already_index = 0;
  2443. }
  2444.  
  2445. /* Get and print the node specified by FILENAME and NODENAME to STREAM.
  2446.    If DUMP_SUBNODES is non-zero, recursively dump the nodes which appear
  2447.    in the menu of each node dumped. */
  2448. static void
  2449. dump_node_to_stream (filename, nodename, stream, dump_subnodes)
  2450.      char *filename, *nodename;
  2451.      FILE *stream;
  2452.      int dump_subnodes;
  2453. {
  2454.   register int i;
  2455.   NODE *node;
  2456.  
  2457.   node = info_get_node (filename, nodename);
  2458.  
  2459.   if (!node)
  2460.     {
  2461.       if (info_recent_file_error)
  2462.     info_error (info_recent_file_error);
  2463.       else
  2464.     {
  2465.       if (filename && *nodename != '(')
  2466.         info_error
  2467.           (CANT_FILE_NODE, filename_non_directory (filename), nodename);
  2468.       else
  2469.         info_error (CANT_FIND_NODE, nodename);
  2470.     }
  2471.       return;
  2472.     }
  2473.  
  2474.   /* If we have already dumped this node, don't dump it again. */
  2475.   for (i = 0; i < dumped_already_index; i++)
  2476.     if (strcmp (node->nodename, dumped_already[i]) == 0)
  2477.       {
  2478.     free (node);
  2479.     return;
  2480.       }
  2481.   add_pointer_to_array (node->nodename, dumped_already_index, dumped_already,
  2482.             dumped_already_slots, 50, char *);
  2483.  
  2484. #if defined (VERBOSE_NODE_DUMPING)
  2485.   /* Maybe we should print some information about the node being output. */
  2486.   if (node->filename)
  2487.     info_error ("Writing node \"(%s)%s\"...",
  2488.         filename_non_directory (node->filename), node->nodename);
  2489.   else
  2490.     info_error ("Writing node \"%s\"...", node->nodename);
  2491. #endif /* VERBOSE_NODE_DUMPING */
  2492.  
  2493.   write_node_to_stream (node, stream);
  2494.  
  2495.   /* If we are dumping subnodes, get the list of menu items in this node,
  2496.      and dump each one recursively. */
  2497.   if (dump_subnodes)
  2498.     {
  2499.       REFERENCE **menu = (REFERENCE **)NULL;
  2500.  
  2501.       /* If this node is an Index, do not dump the menu references. */
  2502.       if (string_in_line ("Index", node->nodename) == -1)
  2503.     menu = info_menu_of_node (node);
  2504.  
  2505.       if (menu)
  2506.     {
  2507.       for (i = 0; menu[i]; i++)
  2508.         {
  2509.           /* We don't dump Info files which are different than the
  2510.          current one. */
  2511.           if (!menu[i]->filename)
  2512.         dump_node_to_stream
  2513.           (filename, menu[i]->nodename, stream, dump_subnodes);
  2514.         }
  2515.       info_free_references (menu);
  2516.     }
  2517.     }
  2518.  
  2519.   free (node);
  2520. }
  2521.  
  2522. /* Dump NODE to FILENAME.  If DUMP_SUBNODES is non-zero, recursively dump
  2523.    the nodes which appear in the menu of each node dumped. */
  2524. void
  2525. dump_node_to_file (node, filename, dump_subnodes)
  2526.      NODE *node;
  2527.      char *filename;
  2528.      int dump_subnodes;
  2529. {
  2530.   FILE *output_stream;
  2531.   char *nodes_filename;
  2532.  
  2533.   /* Get the stream to print this node to.  Special case of an output
  2534.      filename of "-" means to dump the nodes to stdout. */
  2535.   if (strcmp (filename, "-") == 0)
  2536.     output_stream = stdout;
  2537.   else
  2538.     output_stream = fopen (filename, "w");
  2539.  
  2540.   if (!output_stream)
  2541.     {
  2542.       info_error ("Could not create output file \"%s\".", filename);
  2543.       return;
  2544.     }
  2545.  
  2546.   if (node->parent)
  2547.     nodes_filename = node->parent;
  2548.   else
  2549.     nodes_filename = node->filename;
  2550.  
  2551.   initialize_dumping ();
  2552.   dump_node_to_stream
  2553.     (nodes_filename, node->nodename, output_stream, dump_subnodes);
  2554.  
  2555.   if (output_stream != stdout)
  2556.     fclose (output_stream);
  2557.  
  2558. #if defined (VERBOSE_NODE_DUMPING)
  2559.   info_error ("Done.");
  2560. #endif /* VERBOSE_NODE_DUMPING */
  2561. }
  2562.  
  2563. #if !defined (DEFAULT_INFO_PRINT_COMMAND)
  2564. #  define DEFAULT_INFO_PRINT_COMMAND "lpr"
  2565. #endif /* !DEFAULT_INFO_PRINT_COMMAND */
  2566.  
  2567. DECLARE_INFO_COMMAND (info_print_node,
  2568.  "Pipe the contents of this node through INFO_PRINT_COMMAND")
  2569. {
  2570.   print_node (window->node);
  2571. }
  2572.  
  2573. /* Print NODE on a printer piping it into INFO_PRINT_COMMAND. */
  2574. void
  2575. print_node (node)
  2576.      NODE *node;
  2577. {
  2578.   char *print_command, *getenv ();
  2579.   FILE *printer_pipe;
  2580.  
  2581.   print_command = getenv ("INFO_PRINT_COMMAND");
  2582.  
  2583.   if (!print_command || !*print_command)
  2584.     print_command = DEFAULT_INFO_PRINT_COMMAND;
  2585.  
  2586.   printer_pipe = popen (print_command, "w");
  2587.  
  2588.   if (!printer_pipe)
  2589.     {
  2590.       info_error ("Cannot open pipe to \"%s\".", print_command);
  2591.       return;
  2592.     }
  2593.  
  2594. #if defined (VERBOSE_NODE_DUMPING)
  2595.   /* Maybe we should print some information about the node being output. */
  2596.   if (node->filename)
  2597.     info_error ("Printing node \"(%s)%s\"...",
  2598.         filename_non_directory (node->filename), node->nodename);
  2599.   else
  2600.     info_error ("Printing node \"%s\"...", node->nodename);
  2601. #endif /* VERBOSE_NODE_DUMPING */
  2602.  
  2603.   write_node_to_stream (node, printer_pipe);
  2604.   pclose (printer_pipe);
  2605.  
  2606. #if defined (VERBOSE_NODE_DUMPING)
  2607.   info_error ("Done.");
  2608. #endif /* VERBOSE_NODE_DUMPING */
  2609. }
  2610.  
  2611. static void
  2612. write_node_to_stream (node, stream)
  2613.      NODE *node;
  2614.      FILE *stream;
  2615. {
  2616.   fwrite (node->contents, 1, node->nodelen, stream);
  2617. }
  2618.  
  2619. /* **************************************************************** */
  2620. /*                                    */
  2621. /*              Info Searching Commands                */
  2622. /*                                    */
  2623. /* **************************************************************** */
  2624.  
  2625. /* Variable controlling the garbage collection of files briefly visited
  2626.    during searches.  Such files are normally gc'ed, unless they were
  2627.    compressed to begin with.  If this variable is non-zero, it says
  2628.    to gc even those file buffer contents which had to be uncompressed. */
  2629. int gc_compressed_files = 0;
  2630.  
  2631. static void info_gc_file_buffers ();
  2632.  
  2633. static char *search_string = (char *)NULL;
  2634. static int search_string_index = 0;
  2635. static int search_string_size = 0;
  2636. static int isearch_is_active = 0;
  2637.  
  2638. /* Return the file buffer which belongs to WINDOW's node. */
  2639. FILE_BUFFER *
  2640. file_buffer_of_window (window)
  2641.      WINDOW *window;
  2642. {
  2643.   /* If this window has no node, then it has no file buffer. */
  2644.   if (!window->node)
  2645.     return ((FILE_BUFFER *)NULL);
  2646.  
  2647.   if (window->node->parent)
  2648.     return (info_find_file (window->node->parent));
  2649.  
  2650.   if (window->node->filename)
  2651.     return (info_find_file (window->node->filename));
  2652.  
  2653.   return ((FILE_BUFFER *)NULL);
  2654. }
  2655.  
  2656. /* Search for STRING in NODE starting at START.  Return -1 if the string
  2657.    was not found, or the location of the string if it was.  If WINDOW is
  2658.    passed as non-null, set the window's node to be NODE, its point to be
  2659.    the found string, and readjust the window's pagetop.  Final argument
  2660.    DIR says which direction to search in.  If it is positive, search
  2661.    forward, else backwards. */
  2662. long
  2663. info_search_in_node (string, node, start, window, dir)
  2664.      char *string;
  2665.      NODE *node;
  2666.      long start;
  2667.      WINDOW *window;
  2668.      int dir;
  2669. {
  2670.   SEARCH_BINDING binding;
  2671.   long offset;
  2672.  
  2673.   binding.buffer = node->contents;
  2674.   binding.start = start;
  2675.   binding.end = node->nodelen;
  2676.   binding.flags = S_FoldCase;
  2677.  
  2678.   if (dir < 0)
  2679.     {
  2680.       binding.end = 0;
  2681.       binding.flags |= S_SkipDest;
  2682.     }
  2683.  
  2684.   if (binding.start < 0)
  2685.     return (-1);
  2686.  
  2687.   /* For incremental searches, we always wish to skip past the string. */
  2688.   if (isearch_is_active)
  2689.     binding.flags |= S_SkipDest;
  2690.  
  2691.   offset = search (string, &binding);
  2692.  
  2693.   if (offset != -1 && window)
  2694.     {
  2695.       set_remembered_pagetop_and_point (window);
  2696.       if (window->node != node)
  2697.     window_set_node_of_window (window, node);
  2698.       window->point = offset;
  2699.       window_adjust_pagetop (window);
  2700.     }
  2701.   return (offset);
  2702. }
  2703.  
  2704. /* Search NODE, looking for the largest possible match of STRING.  Start the
  2705.    search at START.  Return the absolute position of the match, or -1, if
  2706.    no part of the string could be found. */
  2707. long
  2708. info_target_search_node (node, string, start)
  2709.      NODE *node;
  2710.      char *string;
  2711.      long start;
  2712. {
  2713.   register int i;
  2714.   long offset;
  2715.   char *target;
  2716.  
  2717.   target = savestring (string);
  2718.   i = strlen (target);
  2719.  
  2720.   /* Try repeatedly searching for this string while removing words from
  2721.      the end of it. */
  2722.   while (i)
  2723.     {
  2724.       target[i] = '\0';
  2725.       offset = info_search_in_node (target, node, start, (WINDOW *)NULL, 1);
  2726.  
  2727.       if (offset != -1)
  2728.     break;
  2729.  
  2730.       /* Delete the last word from TARGET. */
  2731.       for (; i && (!whitespace (target[i]) && (target[i] != ',')); i--);
  2732.     }
  2733.   free (target);
  2734.   return (offset);
  2735. }
  2736.  
  2737. /* Search for STRING starting in WINDOW at point.  If the string is found
  2738.    in this node, set point to that position.  Otherwise, get the file buffer
  2739.    associated with WINDOW's node, and search through each node in that file.
  2740.    If the search fails, return non-zero, else zero.  Side-effect window
  2741.    leaving the node and point where the string was found current. */
  2742. static char *last_searched_for_string = (char *)NULL;
  2743. static int
  2744. info_search_internal (string, window, dir)
  2745.      char *string;
  2746.      WINDOW *window;
  2747.      int dir;
  2748. {
  2749.   register int i;
  2750.   FILE_BUFFER *file_buffer;
  2751.   char *initial_nodename;
  2752.   long ret, start = 0;
  2753.  
  2754.   file_buffer = file_buffer_of_window (window);
  2755.   initial_nodename = window->node->nodename;
  2756.  
  2757.   if ((info_last_executed_command == info_search) &&
  2758.       (last_searched_for_string) &&
  2759.       (strcmp (last_searched_for_string, string) == 0))
  2760.     {
  2761.       ret = info_search_in_node
  2762.     (string, window->node, window->point + dir, window, dir);
  2763.     }
  2764.   else
  2765.     {
  2766.       ret = info_search_in_node
  2767.     (string, window->node, window->point, window, dir);
  2768.     }
  2769.  
  2770.   maybe_free (last_searched_for_string);
  2771.   last_searched_for_string = savestring (string);
  2772.  
  2773.   if (ret != -1)
  2774.     {
  2775.       /* We won! */
  2776.       if (!echo_area_is_active && !isearch_is_active)
  2777.     window_clear_echo_area ();
  2778.       return (0);
  2779.     }
  2780.  
  2781.   /* The string wasn't found in the current node.  Search through the
  2782.      window's file buffer, iff the current node is not "*". */
  2783.   if (!file_buffer || (strcmp (initial_nodename, "*") == 0))
  2784.     return (-1);
  2785.  
  2786.   /* If this file has tags, search through every subfile, starting at
  2787.      this node's subfile and node.  Otherwise, search through the
  2788.      file's node list. */
  2789.   if (file_buffer->tags)
  2790.     {
  2791.       register int current_tag, number_of_tags;
  2792.       char *last_subfile;
  2793.       TAG *tag;
  2794.  
  2795.       /* Find number of tags and current tag. */
  2796.       last_subfile = (char *)NULL;
  2797.       for (i = 0; file_buffer->tags[i]; i++)
  2798.     if (strcmp (initial_nodename, file_buffer->tags[i]->nodename) == 0)
  2799.       {
  2800.         current_tag = i;
  2801.         last_subfile = file_buffer->tags[i]->filename;
  2802.       }
  2803.  
  2804.       number_of_tags = i;
  2805.  
  2806.       /* If there is no last_subfile, our tag wasn't found. */
  2807.       if (!last_subfile)
  2808.     return (-1);
  2809.  
  2810.       /* Search through subsequent nodes, wrapping around to the top
  2811.      of the info file until we find the string or return to this
  2812.      window's node and point. */
  2813.       while (1)
  2814.     {
  2815.       NODE *node;
  2816.  
  2817.       /* Allow C-g to quit the search, failing it if pressed. */
  2818.       return_if_control_g (-1);
  2819.  
  2820.       current_tag += dir;
  2821.  
  2822.       if (current_tag < 0)
  2823.         current_tag = number_of_tags - 1;
  2824.       else if (current_tag == number_of_tags)
  2825.         current_tag = 0;
  2826.  
  2827.       tag = file_buffer->tags[current_tag];
  2828.  
  2829.       if (!echo_area_is_active && (last_subfile != tag->filename))
  2830.         {
  2831.           window_message_in_echo_area
  2832.         ("Searching subfile \"%s\"...",
  2833.          filename_non_directory (tag->filename));
  2834.  
  2835.           last_subfile = tag->filename;
  2836.         }
  2837.  
  2838.       node = info_get_node (file_buffer->filename, tag->nodename);
  2839.  
  2840.       if (!node)
  2841.         {
  2842.           /* If not doing i-search... */
  2843.           if (!echo_area_is_active)
  2844.         {
  2845.           if (info_recent_file_error)
  2846.             info_error (info_recent_file_error);
  2847.           else
  2848.             info_error (CANT_FILE_NODE,
  2849.                 filename_non_directory (file_buffer->filename),
  2850.                 tag->nodename);
  2851.         }
  2852.           return (-1);
  2853.         }
  2854.  
  2855.       if (dir < 0)
  2856.         start = tag->nodelen;
  2857.  
  2858.       ret =
  2859.         info_search_in_node (string, node, start, window, dir);
  2860.  
  2861.       /* Did we find the string in this node? */
  2862.       if (ret != -1)
  2863.         {
  2864.           /* Yes!  We win. */
  2865.           remember_window_and_node (window, node);
  2866.           if (!echo_area_is_active)
  2867.         window_clear_echo_area ();
  2868.           return (0);
  2869.         }
  2870.  
  2871.       /* No.  Free this node, and make sure that we haven't passed
  2872.          our starting point. */
  2873.       free (node);
  2874.  
  2875.       if (strcmp (initial_nodename, tag->nodename) == 0)
  2876.         return (-1);
  2877.     }
  2878.     }
  2879.   return (-1);
  2880. }
  2881.  
  2882. DECLARE_INFO_COMMAND (info_search, "Read a string and search for it")
  2883. {
  2884.   char *line, *prompt;
  2885.   int result, old_pagetop;
  2886.   int direction;
  2887.  
  2888.   if (count < 0)
  2889.     direction = -1;
  2890.   else
  2891.     direction = 1;
  2892.  
  2893.   /* Read a string from the user, defaulting the search to SEARCH_STRING. */
  2894.   if (!search_string)
  2895.     {
  2896.       search_string = (char *)xmalloc (search_string_size = 100);
  2897.       search_string[0] = '\0';
  2898.     }
  2899.  
  2900.   prompt = (char *)xmalloc (50 + strlen (search_string));
  2901.  
  2902.   sprintf (prompt, "%s for string [%s]: ",
  2903.        direction < 0 ? "Search backward" : "Search",
  2904.        search_string);
  2905.  
  2906.   line = info_read_in_echo_area (window, prompt);
  2907.   free (prompt);
  2908.  
  2909.   if (!line)
  2910.     {
  2911.       info_abort_key ();
  2912.       return;
  2913.     }
  2914.  
  2915.   if (*line)
  2916.     {
  2917.       if (strlen (line) + 1 > search_string_size)
  2918.     search_string = (char *)
  2919.       xrealloc (search_string, (search_string_size += 50 + strlen (line)));
  2920.  
  2921.       strcpy (search_string, line);
  2922.       search_string_index = strlen (line);
  2923.       free (line);
  2924.     }
  2925.  
  2926.   old_pagetop = active_window->pagetop;
  2927.   result = info_search_internal (search_string, active_window, direction);
  2928.  
  2929.   if (result != 0 && !info_error_was_printed)
  2930.     info_error ("Search failed.");
  2931.   else if (old_pagetop != active_window->pagetop)
  2932.     {
  2933.       int new_pagetop;
  2934.  
  2935.       new_pagetop = active_window->pagetop;
  2936.       active_window->pagetop = old_pagetop;
  2937.       set_window_pagetop (active_window, new_pagetop);
  2938.       if (auto_footnotes_p)
  2939.     info_get_or_remove_footnotes (active_window);
  2940.     }
  2941.  
  2942.   /* Perhaps free the unreferenced file buffers that were searched, but
  2943.      not retained. */
  2944.   info_gc_file_buffers ();
  2945. }
  2946.  
  2947. /* **************************************************************** */
  2948. /*                                    */
  2949. /*            Incremental Searching                */
  2950. /*                                    */
  2951. /* **************************************************************** */
  2952.  
  2953. static void incremental_search ();
  2954.  
  2955. DECLARE_INFO_COMMAND (isearch_forward,
  2956.               "Search interactively for a string as you type it")
  2957. {
  2958.   incremental_search (window, count, key);
  2959. }
  2960.  
  2961. DECLARE_INFO_COMMAND (isearch_backward,
  2962.               "Search interactively for a string as you type it")
  2963. {
  2964.   incremental_search (window, -count, key);
  2965. }
  2966.  
  2967. /* Incrementally search for a string as it is typed. */
  2968. /* The last accepted incremental search string. */
  2969. static char *last_isearch_accepted = (char *)NULL;
  2970.  
  2971. /* The current incremental search string. */
  2972. static char *isearch_string = (char *)NULL;
  2973. static int isearch_string_index = 0;
  2974. static int isearch_string_size = 0;
  2975. static unsigned char isearch_terminate_search_key = ESC;
  2976.  
  2977. /* Structure defining the current state of an incremental search. */
  2978. typedef struct {
  2979.   WINDOW_STATE_DECL;    /* The node, pagetop and point. */
  2980.   int search_index;    /* Offset of the last char in the search string. */
  2981.   int direction;    /* The direction that this search is heading in. */
  2982.   int failing;        /* Whether or not this search failed. */
  2983. } SEARCH_STATE;
  2984.  
  2985. /* Array of search states. */
  2986. static SEARCH_STATE **isearch_states = (SEARCH_STATE **)NULL;
  2987. static int isearch_states_index = 0;
  2988. static int isearch_states_slots = 0;
  2989.  
  2990. /* Push the state of this search. */
  2991. static void
  2992. push_isearch (window, search_index, direction, failing)
  2993.      WINDOW *window;
  2994.      int search_index, direction, failing;
  2995. {
  2996.   SEARCH_STATE *state;
  2997.  
  2998.   state = (SEARCH_STATE *)xmalloc (sizeof (SEARCH_STATE));
  2999.   window_get_state (window, state);
  3000.   state->search_index = search_index;
  3001.   state->direction = direction;
  3002.   state->failing = failing;
  3003.  
  3004.   add_pointer_to_array (state, isearch_states_index, isearch_states,
  3005.             isearch_states_slots, 20, SEARCH_STATE *);
  3006. }
  3007.  
  3008. /* Pop the state of this search to WINDOW, SEARCH_INDEX, and DIRECTION. */
  3009. static void
  3010. pop_isearch (window, search_index, direction, failing)
  3011.      WINDOW *window;
  3012.      int *search_index, *direction, *failing;
  3013. {
  3014.   SEARCH_STATE *state;
  3015.  
  3016.   if (isearch_states_index)
  3017.     {
  3018.       isearch_states_index--;
  3019.       state = isearch_states[isearch_states_index];
  3020.       window_set_state (window, state);
  3021.       *search_index = state->search_index;
  3022.       *direction = state->direction;
  3023.       *failing = state->failing;
  3024.  
  3025.       free (state);
  3026.       isearch_states[isearch_states_index] = (SEARCH_STATE *)NULL;
  3027.     }
  3028. }
  3029.  
  3030. /* Free the memory used by isearch_states. */
  3031. static void
  3032. free_isearch_states ()
  3033. {
  3034.   register int i;
  3035.  
  3036.   for (i = 0; i < isearch_states_index; i++)
  3037.     {
  3038.       free (isearch_states[i]);
  3039.       isearch_states[i] = (SEARCH_STATE *)NULL;
  3040.     }
  3041.   isearch_states_index = 0;
  3042. }
  3043.  
  3044. /* Display the current search in the echo area. */
  3045. static void
  3046. show_isearch_prompt (dir, string, failing_p)
  3047.      int dir;
  3048.      unsigned char *string;
  3049.      int failing_p;
  3050. {
  3051.   register int i;
  3052.   char *prefix, *prompt, *p_rep;
  3053.   int prompt_len, p_rep_index, p_rep_size;
  3054.  
  3055.   if (dir < 0)
  3056.     prefix = "I-search backward: ";
  3057.   else
  3058.     prefix = "I-search: ";
  3059.  
  3060.   p_rep_index = p_rep_size = 0;
  3061.   p_rep = (char *)NULL;
  3062.   for (i = 0; string[i]; i++)
  3063.     {
  3064.       char *rep;
  3065.  
  3066.       switch (string[i])
  3067.     {
  3068.     case ' ': rep = " "; break;
  3069.     case LFD: rep = "\\n"; break;
  3070.     case TAB: rep = "\\t"; break;
  3071.     default:
  3072.       rep = pretty_keyname (string[i]);
  3073.     }
  3074.       if ((p_rep_index + strlen (rep) + 1) >= p_rep_size)
  3075.     p_rep = (char *)xrealloc (p_rep, p_rep_size += 100);
  3076.  
  3077.       strcpy (p_rep + p_rep_index, rep);
  3078.       p_rep_index += strlen (rep);
  3079.     }
  3080.  
  3081.   prompt_len = strlen (prefix) + p_rep_index + 20;
  3082.   prompt = (char *)xmalloc (prompt_len);
  3083.   sprintf (prompt, "%s%s%s", failing_p ? "Failing " : "", prefix,
  3084.        p_rep ? p_rep : "");
  3085.  
  3086.   window_message_in_echo_area ("%s", prompt);
  3087.   maybe_free (p_rep);
  3088.   free (prompt);
  3089.   display_cursor_at_point (active_window);
  3090. }
  3091.  
  3092. static void
  3093. incremental_search (window, count, ignore)
  3094.      WINDOW *window;
  3095.      int count;
  3096.      unsigned char ignore;
  3097. {
  3098.   unsigned char key;
  3099.   int last_search_result, search_result, dir;
  3100.   SEARCH_STATE mystate, orig_state;
  3101.  
  3102.   if (count < 0)
  3103.     dir = -1;
  3104.   else
  3105.     dir = 1;
  3106.  
  3107.   last_search_result = search_result = 0;
  3108.  
  3109.   window_get_state (window, &orig_state);
  3110.  
  3111.   isearch_string_index = 0;
  3112.   if (!isearch_string_size)
  3113.     isearch_string = (char *)xmalloc (isearch_string_size = 50);
  3114.  
  3115.   /* Show the search string in the echo area. */
  3116.   isearch_string[isearch_string_index] = '\0';
  3117.   show_isearch_prompt (dir, isearch_string, search_result);
  3118.  
  3119.   isearch_is_active = 1;
  3120.  
  3121.   while (isearch_is_active)
  3122.     {
  3123.       VFunction *func = (VFunction *)NULL;
  3124.       int quoted = 0;
  3125.  
  3126.       /* If a recent display was interrupted, then do the redisplay now if
  3127.      it is convenient. */
  3128.       if (!info_any_buffered_input_p () && display_was_interrupted_p)
  3129.     {
  3130.       display_update_one_window (window);
  3131.       display_cursor_at_point (active_window);
  3132.     }
  3133.  
  3134.       /* Read a character and dispatch on it. */
  3135.       key = info_get_input_char ();
  3136.       window_get_state (window, &mystate);
  3137.  
  3138.       if (key == DEL)
  3139.     {
  3140.       /* User wants to delete one level of search? */
  3141.       if (!isearch_states_index)
  3142.         {
  3143.           terminal_ring_bell ();
  3144.           continue;
  3145.         }
  3146.       else
  3147.         {
  3148.           pop_isearch
  3149.         (window, &isearch_string_index, &dir, &search_result);
  3150.           isearch_string[isearch_string_index] = '\0';
  3151.           show_isearch_prompt (dir, isearch_string, search_result);
  3152.           goto after_search;
  3153.         }
  3154.     }
  3155.       else if (key == Control ('q'))
  3156.     {
  3157.       key = info_get_input_char ();
  3158.       quoted = 1;
  3159.     }
  3160.  
  3161.       /* We are about to search again, or quit.  Save the current search. */
  3162.       push_isearch (window, isearch_string_index, dir, search_result);
  3163.  
  3164.       if (quoted)
  3165.     goto insert_and_search;
  3166.  
  3167.       if (!Meta_p (key) || (ISO_Latin_p && key < 160))
  3168.     {
  3169.       func = window->keymap[key].function;
  3170.  
  3171.       /* If this key invokes an incremental search, then this means that
  3172.          we will either search again in the same direction, search
  3173.          again in the reverse direction, or insert the last search
  3174.          string that was accepted through incremental searching. */
  3175.       if (func == isearch_forward || func == isearch_backward)
  3176.         {
  3177.           if ((func == isearch_forward && dir > 0) ||
  3178.           (func == isearch_backward && dir < 0))
  3179.         {
  3180.           /* If the user has typed no characters, then insert the
  3181.              last successful search into the current search string. */
  3182.           if (isearch_string_index == 0)
  3183.             {
  3184.               /* Of course, there must be something to insert. */
  3185.               if (last_isearch_accepted)
  3186.             {
  3187.               if (strlen (last_isearch_accepted) + 1 >=
  3188.                   isearch_string_size)
  3189.                 isearch_string = (char *)
  3190.                   xrealloc (isearch_string,
  3191.                     isearch_string_size += 10 +
  3192.                     strlen (last_isearch_accepted));
  3193.               strcpy (isearch_string, last_isearch_accepted);
  3194.               isearch_string_index = strlen (isearch_string);
  3195.               goto search_now;
  3196.             }
  3197.               else
  3198.             continue;
  3199.             }
  3200.           else
  3201.             {
  3202.               /* Search again in the same direction.  This means start
  3203.              from a new place if the last search was successful. */
  3204.               if (search_result == 0)
  3205.             window->point += dir;
  3206.             }
  3207.         }
  3208.           else
  3209.         {
  3210.           /* Reverse the direction of the search. */
  3211.           dir = -dir;
  3212.         }
  3213.         }
  3214.       else if (isprint (key) || func == (VFunction *)NULL)
  3215.         {
  3216.         insert_and_search:
  3217.  
  3218.           if (isearch_string_index + 2 >= isearch_string_size)
  3219.         isearch_string = (char *)xrealloc
  3220.           (isearch_string, isearch_string_size += 100);
  3221.  
  3222.           isearch_string[isearch_string_index++] = key;
  3223.           isearch_string[isearch_string_index] = '\0';
  3224.           goto search_now;
  3225.         }
  3226.       else if (func == info_abort_key)
  3227.         {
  3228.           /* If C-g pressed, and the search is failing, pop the search
  3229.          stack back to the last unfailed search. */
  3230.           if (isearch_states_index && (search_result != 0))
  3231.         {
  3232.           terminal_ring_bell ();
  3233.           while (isearch_states_index && (search_result != 0))
  3234.             pop_isearch
  3235.               (window, &isearch_string_index, &dir, &search_result);
  3236.           isearch_string[isearch_string_index] = '\0';
  3237.           show_isearch_prompt (dir, isearch_string, search_result);
  3238.           continue;
  3239.         }
  3240.           else
  3241.         goto exit_search;
  3242.         }
  3243.       else
  3244.         goto exit_search;
  3245.     }
  3246.       else
  3247.     {
  3248.     exit_search:
  3249.       /* The character is not printable, or it has a function which is
  3250.          non-null.  Exit the search, remembering the search string.  If
  3251.          the key is not the same as the isearch_terminate_search_key,
  3252.          then push it into pending input. */
  3253.       if (isearch_string_index && func != info_abort_key)
  3254.         {
  3255.           maybe_free (last_isearch_accepted);
  3256.           last_isearch_accepted = savestring (isearch_string);
  3257.         }
  3258.  
  3259.       if (key != isearch_terminate_search_key)
  3260.         info_set_pending_input (key);
  3261.  
  3262.       if (func == info_abort_key)
  3263.         {
  3264.           if (isearch_states_index)
  3265.         window_set_state (window, &orig_state);
  3266.         }
  3267.  
  3268.       if (!echo_area_is_active)
  3269.         window_clear_echo_area ();
  3270.  
  3271.       if (auto_footnotes_p)
  3272.         info_get_or_remove_footnotes (active_window);
  3273.  
  3274.       isearch_is_active = 0;
  3275.       continue;
  3276.     }
  3277.  
  3278.       /* Search for the contents of isearch_string. */
  3279.     search_now:
  3280.       show_isearch_prompt (dir, isearch_string, search_result);
  3281.  
  3282.       if (search_result == 0)
  3283.     {
  3284.       /* Check to see if the current search string is right here.  If
  3285.          we are looking at it, then don't bother calling the search
  3286.          function. */
  3287.       if (((dir < 0) &&
  3288.            (strnicmp (window->node->contents + window->point,
  3289.              isearch_string, isearch_string_index) == 0)) ||
  3290.           ((dir > 0) &&
  3291.            ((window->point - isearch_string_index) >= 0) &&
  3292.            (strnicmp (window->node->contents +
  3293.               (window->point - (isearch_string_index - 1)),
  3294.               isearch_string, isearch_string_index) == 0)))
  3295.         {
  3296.           if (dir > 0)
  3297.         window->point++;
  3298.         }
  3299.       else
  3300.         search_result = info_search_internal (isearch_string, window, dir);
  3301.     }
  3302.  
  3303.       /* If this search failed, and we didn't already have a failed search,
  3304.      then ring the terminal bell. */
  3305.       if (search_result != 0 && last_search_result == 0)
  3306.     terminal_ring_bell ();
  3307.  
  3308.     after_search:
  3309.       show_isearch_prompt (dir, isearch_string, search_result);
  3310.  
  3311.       if (search_result == 0)
  3312.     {
  3313.       if ((mystate.node == window->node) &&
  3314.           (mystate.pagetop != window->pagetop))
  3315.         {
  3316.           int newtop = window->pagetop;
  3317.           window->pagetop = mystate.pagetop;
  3318.           set_window_pagetop (window, newtop);
  3319.         }
  3320.       display_update_one_window (window);
  3321.       display_cursor_at_point (window);
  3322.     }
  3323.  
  3324.       last_search_result = search_result;
  3325.     }
  3326.  
  3327.   /* Free the memory used to remember each search state. */
  3328.   free_isearch_states ();
  3329.  
  3330.   /* Perhaps GC some file buffers. */
  3331.   info_gc_file_buffers ();
  3332.  
  3333.   /* After searching, leave the window in the correct state. */
  3334.   if (!echo_area_is_active)
  3335.     window_clear_echo_area ();
  3336. }
  3337.  
  3338. /* GC some file buffers.  A file buffer can be gc-ed if there we have
  3339.    no nodes in INFO_WINDOWS that reference this file buffer's contents.
  3340.    Garbage collecting a file buffer means to free the file buffers
  3341.    contents. */
  3342. static void
  3343. info_gc_file_buffers ()
  3344. {
  3345.   register int fb_index, iw_index, i;
  3346.   register FILE_BUFFER *fb;
  3347.   register INFO_WINDOW *iw;
  3348.  
  3349.   if (!info_loaded_files)
  3350.     return;
  3351.  
  3352.   for (fb_index = 0; fb = info_loaded_files[fb_index]; fb_index++)
  3353.     {
  3354.       int fb_referenced_p = 0;
  3355.  
  3356.       /* If already gc-ed, do nothing. */
  3357.       if (!fb->contents)
  3358.     continue;
  3359.  
  3360.       /* If this file had to be uncompressed, check to see if we should
  3361.      gc it.  This means that the user-variable "gc-compressed-files"
  3362.      is non-zero. */
  3363.       if ((fb->flags & N_IsCompressed) && !gc_compressed_files)
  3364.     continue;
  3365.  
  3366.       /* If this file's contents are not gc-able, move on. */
  3367.       if (fb->flags & N_CannotGC)
  3368.     continue;
  3369.  
  3370.       /* Check each INFO_WINDOW to see if it has any nodes which reference
  3371.      this file. */
  3372.       for (iw_index = 0; iw = info_windows[iw_index]; iw_index++)
  3373.     {
  3374.       for (i = 0; iw->nodes && iw->nodes[i]; i++)
  3375.         {
  3376.           if ((strcmp (fb->fullpath, iw->nodes[i]->filename) == 0) ||
  3377.           (strcmp (fb->filename, iw->nodes[i]->filename) == 0))
  3378.         {
  3379.           fb_referenced_p = 1;
  3380.           break;
  3381.         }
  3382.         }
  3383.     }
  3384.  
  3385.       /* If this file buffer wasn't referenced, free its contents. */
  3386.       if (!fb_referenced_p)
  3387.     {
  3388.       free (fb->contents);
  3389.       fb->contents = (char *)NULL;
  3390.     }
  3391.     }
  3392. }
  3393.  
  3394. /* **************************************************************** */
  3395. /*                                    */
  3396. /*          Traversing and Selecting References            */
  3397. /*                                    */
  3398. /* **************************************************************** */
  3399.  
  3400. /* Move to the next or previous cross reference in this node. */
  3401. static void
  3402. info_move_to_xref (window, count, key, dir)
  3403.      WINDOW *window;
  3404.      int count;
  3405.      unsigned char key;
  3406.      int dir;
  3407. {
  3408.   long firstmenu, firstxref;
  3409.   long nextmenu, nextxref;
  3410.   long placement = -1;
  3411.   long start = 0;
  3412.   NODE *node = window->node;
  3413.  
  3414.   if (dir < 0)
  3415.     start = node->nodelen;
  3416.  
  3417.   /* This search is only allowed to fail if there is no menu or cross
  3418.      reference in the current node.  Otherwise, the first menu or xref
  3419.      found is moved to. */
  3420.  
  3421.   firstmenu = info_search_in_node
  3422.     (INFO_MENU_ENTRY_LABEL, node, start, (WINDOW *)NULL, dir);
  3423.  
  3424.   /* FIRSTMENU may point directly to the line defining the menu.  Skip that
  3425.      and go directly to the first item. */
  3426.  
  3427.   if (firstmenu != -1)
  3428.     {
  3429.       char *text = node->contents + firstmenu;
  3430.  
  3431.       if (strncmp (text, INFO_MENU_LABEL, strlen (INFO_MENU_LABEL)) == 0)
  3432.     firstmenu = info_search_in_node
  3433.       (INFO_MENU_ENTRY_LABEL, node, firstmenu + dir, (WINDOW *)NULL, dir);
  3434.     }
  3435.  
  3436.   firstxref =
  3437.     info_search_in_node (INFO_XREF_LABEL, node, start, (WINDOW *)NULL, dir);
  3438.  
  3439.   if (firstmenu == -1 && firstxref == -1)
  3440.     {
  3441.       info_error ("No cross references in this node.");
  3442.       return;
  3443.     }
  3444.  
  3445.   /* There is at least one cross reference or menu entry in this node.
  3446.      Try hard to find the next available one. */
  3447.  
  3448.   nextmenu = info_search_in_node
  3449.     (INFO_MENU_ENTRY_LABEL, node, window->point + dir, (WINDOW *)NULL, dir);
  3450.  
  3451.   nextxref = info_search_in_node
  3452.     (INFO_XREF_LABEL, node, window->point + dir, (WINDOW *)NULL, dir);
  3453.  
  3454.   /* Ignore "Menu:" as a menu item. */
  3455.   if (nextmenu != -1)
  3456.     {
  3457.       char *text = node->contents + nextmenu;
  3458.  
  3459.       if (strncmp (text, INFO_MENU_LABEL, strlen (INFO_MENU_LABEL)) == 0)
  3460.     nextmenu = info_search_in_node
  3461.       (INFO_MENU_ENTRY_LABEL, node, nextmenu + dir, (WINDOW *)NULL, dir);
  3462.     }
  3463.  
  3464.   /* If there is both a next menu entry, and a next xref entry, choose the
  3465.      one which occurs first.  Otherwise, select the one which actually
  3466.      appears in this node following point. */
  3467.   if (nextmenu != -1 && nextxref != -1)
  3468.     {
  3469.       if (((dir == 1) && (nextmenu < nextxref)) ||
  3470.       ((dir == -1) && (nextmenu > nextxref)))
  3471.     placement = nextmenu + 1;
  3472.       else
  3473.     placement = nextxref;
  3474.     }
  3475.   else if (nextmenu != -1)
  3476.     placement = nextmenu + 1;
  3477.   else if (nextxref != -1)
  3478.     placement = nextxref;
  3479.  
  3480.   /* If there was neither a menu or xref entry appearing in this node after
  3481.      point, choose the first menu or xref entry appearing in this node. */
  3482.   if (placement == -1)
  3483.     {
  3484.       if (firstmenu != -1 && firstxref != -1)
  3485.     {
  3486.       if (((dir == 1) && (firstmenu < firstxref)) ||
  3487.           ((dir == -1) && (firstmenu > firstxref)))
  3488.         placement = firstmenu + 1;
  3489.       else
  3490.         placement = firstxref;
  3491.     }
  3492.       else if (firstmenu != -1)
  3493.     placement = firstmenu + 1;
  3494.       else
  3495.     placement = firstxref;
  3496.     }
  3497.   window->point = placement;
  3498.   window_adjust_pagetop (window);
  3499.   window->flags |= W_UpdateWindow;
  3500. }
  3501.  
  3502. DECLARE_INFO_COMMAND (info_move_to_prev_xref,
  3503.               "Move to the previous cross reference")
  3504. {
  3505.   if (count < 0)
  3506.     info_move_to_prev_xref (window, -count, key);
  3507.   else
  3508.     info_move_to_xref (window, count, key, -1);
  3509. }
  3510.  
  3511. DECLARE_INFO_COMMAND (info_move_to_next_xref,
  3512.               "Move to the next cross reference")
  3513. {
  3514.   if (count < 0)
  3515.     info_move_to_next_xref (window, -count, key);
  3516.   else
  3517.     info_move_to_xref (window, count, key, 1);
  3518. }
  3519.  
  3520. /* Select the menu item or reference that appears on this line. */
  3521. DECLARE_INFO_COMMAND (info_select_reference_this_line,
  3522.               "Select reference or menu item appearing on this line")
  3523. {
  3524.   char *line;
  3525.   NODE *orig;
  3526.  
  3527.   line = window->line_starts[window_line_of_point (window)];
  3528.   orig = window->node;
  3529.  
  3530.   /* If this line contains a menu item, select that one. */
  3531.   if (strncmp ("* ", line, 2) == 0)
  3532.     info_menu_or_ref_item (window, count, key, info_menu_of_node, 0);
  3533.   else
  3534.     info_menu_or_ref_item (window, count, key, info_xrefs_of_node, 0);
  3535. }
  3536.  
  3537. /* **************************************************************** */
  3538. /*                                    */
  3539. /*            Miscellaneous Info Commands                */
  3540. /*                                    */
  3541. /* **************************************************************** */
  3542.  
  3543. /* What to do when C-g is pressed in a window. */
  3544. DECLARE_INFO_COMMAND (info_abort_key, "Cancel current operation")
  3545. {
  3546.   /* If error printing doesn't oridinarily ring the bell, do it now,
  3547.      since C-g always rings the bell.  Otherwise, let the error printer
  3548.      do it. */
  3549.   if (!info_error_rings_bell_p)
  3550.     terminal_ring_bell ();
  3551.   info_error ("Quit");
  3552.  
  3553.   info_initialize_numeric_arg ();
  3554.   info_clear_pending_input ();
  3555.   info_last_executed_command = (VFunction *)NULL;
  3556. }
  3557.  
  3558. /* Move the cursor to the desired line of the window. */
  3559. DECLARE_INFO_COMMAND (info_move_to_window_line,
  3560.    "Move to the cursor to a specific line of the window")
  3561. {
  3562.   int line;
  3563.  
  3564.   /* With no numeric argument of any kind, default to the center line. */
  3565.   if (!info_explicit_arg && count == 1)
  3566.     line = (window->height / 2) + window->pagetop;
  3567.   else
  3568.     {
  3569.       if (count < 0)
  3570.     line = (window->height + count) + window->pagetop;
  3571.       else
  3572.     line = window->pagetop + count;
  3573.     }
  3574.  
  3575.   /* If the line doesn't appear in this window, make it do so. */
  3576.   if ((line - window->pagetop) >= window->height)
  3577.     line = window->pagetop + (window->height - 1);
  3578.  
  3579.   /* If the line is too small, make it fit. */
  3580.   if (line < window->pagetop)
  3581.     line = window->pagetop;
  3582.  
  3583.   /* If the selected line is past the bottom of the node, force it back. */
  3584.   if (line >= window->line_count)
  3585.     line = window->line_count - 1;
  3586.  
  3587.   window->point = (window->line_starts[line] - window->node->contents);
  3588. }
  3589.  
  3590. /* Clear the screen and redraw its contents.  Given a numeric argument,
  3591.    move the line the cursor is on to the COUNT'th line of the window. */
  3592. DECLARE_INFO_COMMAND (info_redraw_display, "Redraw the display")
  3593. {
  3594.   if ((!info_explicit_arg && count == 1) || echo_area_is_active)
  3595.     {
  3596.       terminal_clear_screen ();
  3597.       display_clear_display (the_display);
  3598.       window_mark_chain (windows, W_UpdateWindow);
  3599.       display_update_display (windows);
  3600.     }
  3601.   else
  3602.     {
  3603.       int desired_line, point_line;
  3604.       int new_pagetop;
  3605.  
  3606.       point_line = window_line_of_point (window) - window->pagetop;
  3607.  
  3608.       if (count < 0)
  3609.     desired_line = window->height + count;
  3610.       else
  3611.     desired_line = count;
  3612.  
  3613.       if (desired_line < 0)
  3614.     desired_line = 0;
  3615.  
  3616.       if (desired_line >= window->height)
  3617.     desired_line = window->height - 1;
  3618.  
  3619.       if (desired_line == point_line)
  3620.     return;
  3621.  
  3622.       new_pagetop = window->pagetop + (point_line - desired_line);
  3623.  
  3624.       set_window_pagetop (window, new_pagetop);
  3625.     }
  3626. }
  3627. /* This command does nothing.  It is the fact that a key is bound to it
  3628.    that has meaning.  See the code at the top of info_session (). */
  3629. DECLARE_INFO_COMMAND (info_quit, "Quit using Info")
  3630. {}
  3631.  
  3632.  
  3633. /* **************************************************************** */
  3634. /*                                    */
  3635. /*         Reading Keys and Dispatching on Them            */
  3636. /*                                    */
  3637. /* **************************************************************** */
  3638.  
  3639. /* Declaration only.  Special cased in info_dispatch_on_key (). */
  3640. DECLARE_INFO_COMMAND (info_do_lowercase_version, "")
  3641. {}
  3642.  
  3643. static void
  3644. dispatch_error (keyseq)
  3645.      char *keyseq;
  3646. {
  3647.   char *rep;
  3648.  
  3649.   rep = pretty_keyseq (keyseq);
  3650.  
  3651.   if (!echo_area_is_active)
  3652.     info_error ("Unknown command (%s).", rep);
  3653.   else
  3654.     {
  3655.       char *temp;
  3656.  
  3657.       temp = (char *)xmalloc (1 + strlen (rep) + strlen ("\"\" is invalid"));
  3658.  
  3659.       sprintf (temp, "\"%s\" is invalid", rep);
  3660.       terminal_ring_bell ();
  3661.       inform_in_echo_area (temp);
  3662.       free (temp);
  3663.     }
  3664. }
  3665.  
  3666. /* Keeping track of key sequences. */
  3667. static char *info_keyseq = (char *)NULL;
  3668. static char keyseq_rep[100];
  3669. static int info_keyseq_index = 0;
  3670. static int info_keyseq_size = 0;
  3671. static int info_keyseq_displayed_p = 0;
  3672.  
  3673. /* Initialize the length of the current key sequence. */
  3674. void
  3675. initialize_keyseq ()
  3676. {
  3677.   info_keyseq_index = 0;
  3678.   info_keyseq_displayed_p = 0;
  3679. }
  3680.  
  3681. /* Add CHARACTER to the current key sequence. */
  3682. void
  3683. add_char_to_keyseq (character)
  3684.      char character;
  3685. {
  3686.   if (info_keyseq_index + 2 >= info_keyseq_size)
  3687.     info_keyseq = (char *)xrealloc (info_keyseq, info_keyseq_size += 10);
  3688.  
  3689.   info_keyseq[info_keyseq_index++] = character;
  3690.   info_keyseq[info_keyseq_index] = '\0';
  3691. }
  3692.  
  3693. /* Return the pretty printable string which represents KEYSEQ. */
  3694. char *
  3695. pretty_keyseq (keyseq)
  3696.      char *keyseq;
  3697. {
  3698.   register int i;
  3699.  
  3700.   keyseq_rep[0] = '\0';
  3701.  
  3702.   for (i = 0; keyseq[i]; i++)
  3703.     {
  3704.       sprintf (keyseq_rep + strlen (keyseq_rep), "%s%s",
  3705.            strlen (keyseq_rep) ? " " : "",
  3706.            pretty_keyname (keyseq[i]));
  3707.     }
  3708.  
  3709.   return (keyseq_rep);
  3710. }
  3711.  
  3712. /* Display the current value of info_keyseq.  If argument EXPECTING is
  3713.    non-zero, input is expected to be read after the key sequence is
  3714.    displayed, so add an additional prompting character to the sequence. */
  3715. void
  3716. display_info_keyseq (expecting_future_input)
  3717.      int expecting_future_input;
  3718. {
  3719.   char *rep;
  3720.  
  3721.   rep = pretty_keyseq (info_keyseq);
  3722.   if (expecting_future_input)
  3723.     strcat (rep, "-");
  3724.  
  3725.   if (echo_area_is_active)
  3726.     inform_in_echo_area (rep);
  3727.   else
  3728.     {
  3729.       window_message_in_echo_area (rep);
  3730.       display_cursor_at_point (active_window);
  3731.     }
  3732.   info_keyseq_displayed_p = 1;
  3733. }
  3734.  
  3735. /* Called by interactive commands to read a keystroke. */
  3736. unsigned char
  3737. info_get_another_input_char ()
  3738. {
  3739.   int ready = 0;
  3740.  
  3741.   /* If there isn't any input currently available, then wait a
  3742.      moment looking for input.  If we don't get it fast enough,
  3743.      prompt a little bit with the current key sequence. */
  3744.   if (!info_keyseq_displayed_p &&
  3745.       !info_any_buffered_input_p () &&
  3746.       !info_input_pending_p ())
  3747.     {
  3748. #if defined (FD_SET)
  3749.       struct timeval timer;
  3750.       fd_set readfds;
  3751.  
  3752.       FD_ZERO (&readfds);
  3753.       FD_SET (fileno (info_input_stream), &readfds);
  3754.       timer.tv_sec = 1;
  3755.       timer.tv_usec = 750;
  3756.       ready = select (1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timer);
  3757. #endif /* FD_SET */
  3758.     }
  3759.  
  3760.   if (!ready)
  3761.     display_info_keyseq (1);
  3762.  
  3763.   return (info_get_input_char ());
  3764. }
  3765.  
  3766. /* Do the command associated with KEY in MAP.  If the associated command is
  3767.    really a keymap, then read another key, and dispatch into that map. */
  3768. void
  3769. info_dispatch_on_key (key, map)
  3770.      unsigned char key;
  3771.      Keymap map;
  3772. {
  3773.   if (Meta_p (key) && (!ISO_Latin_p || map[key].function != ea_insert))
  3774.     {
  3775.       if (map[ESC].type == ISKMAP)
  3776.     {
  3777.       map = (Keymap)map[ESC].function;
  3778.       add_char_to_keyseq (ESC);
  3779.       key = UnMeta (key);
  3780.       info_dispatch_on_key (key, map);
  3781.     }
  3782.       else
  3783.     {
  3784.       dispatch_error (info_keyseq);
  3785.     }
  3786.       return;
  3787.     }
  3788.  
  3789.   switch (map[key].type)
  3790.     {
  3791.     case ISFUNC:
  3792.       {
  3793.     VFunction *func;
  3794.  
  3795.     func = map[key].function;
  3796.     if (func != (VFunction *)NULL)
  3797.       {
  3798.         /* Special case info_do_lowercase_version (). */
  3799.         if (func == info_do_lowercase_version)
  3800.           {
  3801.         info_dispatch_on_key (tolower (key), map);
  3802.         return;
  3803.           }
  3804.  
  3805.         add_char_to_keyseq (key);
  3806.  
  3807.         if (info_keyseq_displayed_p)
  3808.           display_info_keyseq (0);
  3809.  
  3810.         {
  3811.           WINDOW *where;
  3812.  
  3813.           where = active_window;
  3814.           (*map[key].function)
  3815.         (active_window, info_numeric_arg * info_numeric_arg_sign, key);
  3816.  
  3817.           /* If we have input pending, then the last command was a prefix
  3818.          command.  Don't change the value of the last function vars.
  3819.          Otherwise, remember the last command executed in the var
  3820.          appropriate to the window in which it was executed. */
  3821.           if (!info_input_pending_p ())
  3822.         {
  3823.           if (where == the_echo_area)
  3824.             ea_last_executed_command = map[key].function;
  3825.           else
  3826.             info_last_executed_command = map[key].function;
  3827.         }
  3828.         }
  3829.       }
  3830.     else
  3831.       {
  3832.         add_char_to_keyseq (key);
  3833.         dispatch_error (info_keyseq);
  3834.         return;
  3835.       }
  3836.       }
  3837.       break;
  3838.  
  3839.     case ISKMAP:
  3840.       add_char_to_keyseq (key);
  3841.       if (map[key].function != (VFunction *)NULL)
  3842.     {
  3843.       unsigned char newkey;
  3844.  
  3845.       newkey = info_get_another_input_char ();
  3846.       info_dispatch_on_key (newkey, (Keymap)map[key].function);
  3847.     }
  3848.       else
  3849.     {
  3850.       dispatch_error (info_keyseq);
  3851.       return;
  3852.     }
  3853.       break;
  3854.     }
  3855. }
  3856.  
  3857. /* **************************************************************** */
  3858. /*                                    */
  3859. /*            Numeric Arguments                */
  3860. /*                                    */
  3861. /* **************************************************************** */
  3862.  
  3863. /* Handle C-u style numeric args, as well as M--, and M-digits. */
  3864.  
  3865. /* Non-zero means that an explicit argument has been passed to this
  3866.    command, as in C-u C-v. */
  3867. int info_explicit_arg = 0;
  3868.  
  3869. /* The sign of the numeric argument. */
  3870. int info_numeric_arg_sign = 1;
  3871.  
  3872. /* The value of the argument itself. */
  3873. int info_numeric_arg = 1;
  3874.  
  3875. /* Add the current digit to the argument in progress. */
  3876. DECLARE_INFO_COMMAND (info_add_digit_to_numeric_arg,
  3877.               "Add this digit to the current numeric argument")
  3878. {
  3879.   info_numeric_arg_digit_loop (window, 0, key);
  3880. }
  3881.  
  3882. /* C-u, universal argument.  Multiply the current argument by 4.
  3883.    Read a key.  If the key has nothing to do with arguments, then
  3884.    dispatch on it.  If the key is the abort character then abort. */
  3885. DECLARE_INFO_COMMAND (info_universal_argument,
  3886.               "Start (or multiply by 4) the current numeric argument")
  3887. {
  3888.   info_numeric_arg *= 4;
  3889.   info_numeric_arg_digit_loop (window, 0, 0);
  3890. }
  3891.  
  3892. /* Create a default argument. */
  3893. void
  3894. info_initialize_numeric_arg ()
  3895. {
  3896.   info_numeric_arg = info_numeric_arg_sign = 1;
  3897.   info_explicit_arg = 0;
  3898. }
  3899.  
  3900. DECLARE_INFO_COMMAND (info_numeric_arg_digit_loop, "")
  3901. {
  3902.   unsigned char pure_key;
  3903.   Keymap keymap = window->keymap;
  3904.  
  3905.   while (1)
  3906.     {
  3907.       if (key)
  3908.     pure_key = key;
  3909.       else
  3910.     {
  3911.       if (display_was_interrupted_p && !info_any_buffered_input_p ())
  3912.         display_update_display (windows);
  3913.  
  3914.       if (active_window != the_echo_area)
  3915.         display_cursor_at_point (active_window);
  3916.  
  3917.       pure_key = key = info_get_another_input_char ();
  3918.  
  3919.       if (Meta_p (key))
  3920.         add_char_to_keyseq (ESC);
  3921.  
  3922.       add_char_to_keyseq (UnMeta (key));
  3923.     }
  3924.  
  3925.       if (Meta_p (key))
  3926.     key = UnMeta (key);
  3927.  
  3928.       if (keymap[key].type == ISFUNC &&
  3929.       keymap[key].function == info_universal_argument)
  3930.     {
  3931.       info_numeric_arg *= 4;
  3932.       key = 0;
  3933.       continue;
  3934.     }
  3935.  
  3936.       if (isdigit (key))
  3937.     {
  3938.       if (info_explicit_arg)
  3939.         info_numeric_arg = (info_numeric_arg * 10) + (key - '0');
  3940.       else
  3941.         info_numeric_arg = (key - '0');
  3942.       info_explicit_arg = 1;
  3943.     }
  3944.       else
  3945.     {
  3946.       if (key == '-' && !info_explicit_arg)
  3947.         {
  3948.           info_numeric_arg_sign = -1;
  3949.           info_numeric_arg = 1;
  3950.         }
  3951.       else
  3952.         {
  3953.           info_keyseq_index--;
  3954.           info_dispatch_on_key (pure_key, keymap);
  3955.           return;
  3956.         }
  3957.     }
  3958.       key = 0;
  3959.     }
  3960. }
  3961.  
  3962. /* **************************************************************** */
  3963. /*                                    */
  3964. /*            Input Character Buffering               */
  3965. /*                                    */
  3966. /* **************************************************************** */
  3967.  
  3968. /* Character waiting to be read next. */
  3969. static int pending_input_character = 0;
  3970.  
  3971. /* How to make there be no pending input. */
  3972. static void
  3973. info_clear_pending_input ()
  3974. {
  3975.   pending_input_character = 0;
  3976. }
  3977.  
  3978. /* How to set the pending input character. */
  3979. static void
  3980. info_set_pending_input (key)
  3981.      unsigned char key;
  3982. {
  3983.   pending_input_character = key;
  3984. }
  3985.  
  3986. /* How to see if there is any pending input. */
  3987. unsigned char
  3988. info_input_pending_p ()
  3989. {
  3990.   return (pending_input_character);
  3991. }
  3992.  
  3993. /* Largest number of characters that we can read in advance. */
  3994. #define MAX_INFO_INPUT_BUFFERING 512
  3995.  
  3996. static int pop_index = 0, push_index = 0;
  3997. static unsigned char info_input_buffer[MAX_INFO_INPUT_BUFFERING];
  3998.  
  3999. /* Add KEY to the buffer of characters to be read. */
  4000. static void
  4001. info_push_typeahead (key)
  4002.      unsigned char key;
  4003. {
  4004.   /* Flush all pending input in the case of C-g pressed. */
  4005.   if (key == Control ('g'))
  4006.     {
  4007.       push_index = pop_index;
  4008.       info_set_pending_input (Control ('g'));
  4009.     }
  4010.   else
  4011.     {
  4012.       info_input_buffer[push_index++] = key;
  4013.       if (push_index >= sizeof (info_input_buffer))
  4014.     push_index = 0;
  4015.     }
  4016. }
  4017.  
  4018. /* Return the amount of space available in INFO_INPUT_BUFFER for new chars. */
  4019. static int
  4020. info_input_buffer_space_available ()
  4021. {
  4022.   if (pop_index > push_index)
  4023.     return (pop_index - push_index);
  4024.   else
  4025.     return (sizeof (info_input_buffer - (push_index - pop_index)));
  4026. }
  4027.  
  4028. /* Get a key from the buffer of characters to be read.
  4029.    Return the key in KEY.
  4030.    Result is non-zero if there was a key, or 0 if there wasn't. */
  4031. static int
  4032. info_get_key_from_typeahead (key)
  4033.      unsigned char *key;
  4034. {
  4035.   if (push_index == pop_index)
  4036.     return (0);
  4037.  
  4038.   *key = info_input_buffer[pop_index++];
  4039.  
  4040.   if (pop_index >= sizeof (info_input_buffer))
  4041.     pop_index = 0;
  4042.  
  4043.   return (1);
  4044. }
  4045.  
  4046. int
  4047. info_any_buffered_input_p ()
  4048. {
  4049.   info_gather_typeahead ();
  4050.   return (push_index != pop_index);
  4051. }
  4052.  
  4053. /* Push KEY into the *front* of the input buffer.  Returns non-zero if
  4054.    successful, zero if there is no space left in the buffer. */
  4055. static int
  4056. info_replace_key_to_typeahead (key)
  4057.      unsigned char key;
  4058. {
  4059.   if (info_input_buffer_space_available ())
  4060.     {
  4061.       pop_index--;
  4062.       if (pop_index < 0)
  4063.     pop_index = sizeof (info_input_buffer) - 1;
  4064.       info_input_buffer[pop_index] = key;
  4065.       return (1);
  4066.     }
  4067.   return (0);
  4068. }
  4069.  
  4070. /* If characters are available to be read, then read them and stuff them into
  4071.    info_input_buffer.  Otherwise, do nothing. */
  4072. void
  4073. info_gather_typeahead ()
  4074. {
  4075.   int tty, space_avail;
  4076.   long chars_avail;
  4077.   unsigned char input[MAX_INFO_INPUT_BUFFERING];
  4078.  
  4079.   tty = fileno (info_input_stream);
  4080.   chars_avail = 0;
  4081.  
  4082.   space_avail = info_input_buffer_space_available ();
  4083.  
  4084.   /* If we can just find out how many characters there are to read, do so. */
  4085. #if defined (FIONREAD)
  4086.   {
  4087.     ioctl (tty, FIONREAD, &chars_avail);
  4088.  
  4089.     if (chars_avail > space_avail)
  4090.       chars_avail = space_avail;
  4091.  
  4092.     if (chars_avail)
  4093.       read (tty, &input[0], chars_avail);
  4094.   }
  4095. #else /* !FIONREAD */
  4096. #  if defined (O_NDELAY)
  4097.   {
  4098.     int flags;
  4099.  
  4100.     flags = fcntl (tty, F_GETFL, 0);
  4101.  
  4102.     fcntl (tty, F_SETFL, (flags | O_NDELAY));
  4103.       chars_avail = read (tty, &input[0], space_avail);
  4104.     fcntl (tty, F_SETFL, flags);
  4105.  
  4106.     if (chars_avail == -1)
  4107.       chars_avail = 0;
  4108.   }
  4109. #  endif /* O_NDELAY */
  4110. #endif /* !FIONREAD */
  4111.  
  4112.   /* Store the input characters just read into our input buffer. */
  4113.   {
  4114.     register int i;
  4115.  
  4116.     for (i = 0; i < chars_avail; i++)
  4117.       info_push_typeahead (input[i]);
  4118.   }
  4119. }
  4120.  
  4121. /* How to read a single character. */
  4122. unsigned char
  4123. info_get_input_char ()
  4124. {
  4125.   unsigned char keystroke;
  4126.  
  4127.   info_gather_typeahead ();
  4128.  
  4129.   if (pending_input_character)
  4130.     {
  4131.       keystroke = pending_input_character;
  4132.       pending_input_character = 0;
  4133.     }
  4134.   else if (info_get_key_from_typeahead (&keystroke) == 0)
  4135.     {
  4136.       int rawkey;
  4137.  
  4138.       rawkey = getc (info_input_stream);
  4139.       keystroke = rawkey;
  4140.  
  4141.       if (rawkey == EOF)
  4142.     {
  4143.       if (info_input_stream != stdin)
  4144.         {
  4145.           fclose (info_input_stream);
  4146.           info_input_stream = stdin;
  4147.           display_inhibited = 0;
  4148.           display_update_display (windows);
  4149.           display_cursor_at_point (active_window);
  4150.           rawkey = getc (info_input_stream);
  4151.           keystroke = rawkey;
  4152.         }
  4153.  
  4154.       if (rawkey == EOF)
  4155.         {
  4156.           terminal_unprep_terminal ();
  4157.           close_dribble_file ();
  4158.           exit (0);
  4159.         }
  4160.     }
  4161.     }
  4162.  
  4163.   if (info_dribble_file)
  4164.     dribble (keystroke);
  4165.  
  4166.   return (keystroke);
  4167. }
  4168.